LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-28-2004, 02:02 PM   #1
Yntarius
LQ Newbie
 
Registered: May 2004
Location: Sao Paulo, Brazil
Distribution: RedHat 7.3
Posts: 19

Rep: Reputation: 0
Flex for repetitive patterns


Hi everyone

Im trying to make a program that will get a list of names, separated by eighter "," or ";" or" "enter" and filter the names that already have shown up, putting all the others in another txt.
ie:
it goes from : Ariel, Jonathan, Antonio, Ariel, Jonathan, Clara

to: Ariel, Jonathan, Antonio, Clara

My friend told me ot use flex, but im not having much sucess. This is a copy of what i've done:

********************************************************************************************
%option noyywrap

%{
%}

DATA [a-zA-Z0-9_"."]+

%%

{DATA} { FILE *f = fopen("saida.txt", "a" );
FILE *f_old = fopen("teste.txt", "r");
if( ( f ) && ( f != f_old ) )
{
f_old = f;
fprintf( f, "%s\n", yytext );
fclose( f );
}
}

%%

main( int argc, char **argv )
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;

yylex();
}

********************************************************************************************

I copied most of it from the man page for flex

Anyways, it wont work. It will only filter a few names, i coudltn quite figuere out exactly why.

Please help.

Thanks

Ariel
 
Old 05-28-2004, 02:44 PM   #2
g4c9z
Member
 
Registered: May 2004
Location: Canada
Distribution: Gentoo
Posts: 36

Rep: Reputation: 15
I'm not sure why your friend recommended you use flex for that. Flex recongnizes patterns easily in text and does something with the captured text. Finding duplicates in flex seems hard. Do you know C++? If so, I'd say the easiest way to do it would be to use the standard library: load the names into a vector, then sort the vector, then remove duplicates from it (I forget the exact function calls for this; tell me if you want to do it this way). This exact method wouldn't work if it's important for the names to be in the same order as at the start.

I don't know if this helps; maybe I'm missing something. It's just that flex would be one of the last things I'd think to use to write that program.
 
Old 05-28-2004, 03:17 PM   #3
Yntarius
LQ Newbie
 
Registered: May 2004
Location: Sao Paulo, Brazil
Distribution: RedHat 7.3
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by g4c9z
I'm not sure why your friend recommended you use flex for that. Flex recongnizes patterns easily in text and does something with the captured text. Finding duplicates in flex seems hard. Do you know C++? If so, I'd say the easiest way to do it would be to use the standard library: load the names into a vector, then sort the vector, then remove duplicates from it (I forget the exact function calls for this; tell me if you want to do it this way). This exact method wouldn't work if it's important for the names to be in the same order as at the start.

I don't know if this helps; maybe I'm missing something. It's just that flex would be one of the last things I'd think to use to write that program.
Well, the order doesnt really matters, and i also dont have to use flex. My friend just recomended it to me because he said its a tool that does searches for patterns, he doesnt know it that well too.

I know a thing or 2 on C++, it would be usefull if u gave me a hand doing it, specially the part of opening the file, saving the string to a array or something (i dont remember how to do it).
The rest is easy.. hehehe

Thanks again

Ariel
 
Old 05-28-2004, 07:15 PM   #4
g4c9z
Member
 
Registered: May 2004
Location: Canada
Distribution: Gentoo
Posts: 36

Rep: Reputation: 15
For a complete reference of the standard library, a good place is probably:

http://www.cplusplus.com

If you don't mind buying a book on it, one I really like is "The C++ Standard Library" by Nicolai Josuttis.

Anyway, file I/O is really easy in C++: all you have to do to open a file is:

ifstream f(filename);

Then you read from it using the regular >> operator or the "std::getline" function.

Strings are handled with the "string" class.

The part I was talking about - using a vector, then sorting it and removing duplicates, is done something like this:

#include <algorithm>
#include <string>
#include <vector>

vector<string> names;

//remove duplicates
stable_sort(names.begin(), names.end());
names.erase(
unique(names.begin(), names.end()),
names.end()
);

Don't forget to put "using namespace std" at the start of your program: otherwise you'll get weird error messages!

It's an interesting co-incidence that I've done something similar to what you're trying to do. I wrote a program to manage my meal planning, and part of it is to generate a grocery list. It extracts all the ingredients from a set of recipes and makes a big list of them, but has to remove duplicates. In case it's useful to you as an example, here is the whole function. Feel free to use the code as you wish - as long as you don't call it your own .

/*Produce a grocery list from the ingredients of all the recipes selected
and from a file of ingredients that are always added*/
void groceryList(string infile, //recipes.txt, the list of recipes selected
string outfile, //The file that will contain the grocery list
string rcp_path) //Passed along from the caller
{
vector<string> allRecipes;
std::ifstream frecipes(infile.c_str());
string recipeName;
while (std::getline(frecipes, recipeName))
allRecipes.push_back(recipeName);

vector<string> ingredients;

//fill up ingredients with the ingredients of all recipes chosen randomly
for (int i = 0; i < allRecipes.size(); i++) {
Recipe recipe(allRecipes[i]);

string filename = rcp_path + "\\" + allRecipes[i] + ".rcp";
ifstream recipeFile(filename.c_str());
recipeFile >> recipe;

for (int j = 0; j < recipe.ingreds.size(); j++)
ingredients.push_back(recipe.ingreds.at(j).name);
}

/*Add the regular ingredients - ingredients I eat for a snack, for example,
that I always want on my grocery list*/
string filename = rcp_path + "\\extras\\reg_ingredients.txt";
string input;
ifstream regIngreds(filename.c_str());
while (!regIngreds.fail() && !regIngreds.eof()) {
getline(regIngreds, input);
ingredients.push_back(input);
}

//remove duplicates
stable_sort(ingredients.begin(), ingredients.end());
ingredients.erase(
unique(ingredients.begin(), ingredients.end()),
ingredients.end()
);

//output to a file
ofstream ingredients_txt(outfile.c_str());
ingredients_txt << "Adam's grocery list" << endl << endl;
for (i = 0; i < ingredients.size(); i++)
ingredients_txt << ingredients[i] << endl;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM
giFT - Not a Repetitive Question inescapeableus Linux - Software 0 07-22-2004 02:24 PM
Quick script for running repetitive commands david_ross LinuxQuestions.org Member Success Stories 4 01-25-2004 09:12 AM
repetitive conditional include() in PHP linuxfond Programming 12 09-17-2003 03:51 AM
irritating, repetitive HD sound (only in Linux) aethereal Linux - General 5 11-20-2001 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration