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 |
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. |
Quote:
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 |
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; } |
All times are GMT -5. The time now is 04:22 AM. |