On Jan 8, 2013, at 4:28 AM, punit jain wrote: > Hi , > > I have a file as below : - > > { > test = ("test123"); > test = ("test123","abc"); > test = ("test123","abc","xyz"); > } > { > test1 = ("passfile"); > test1 = ("passfile","pasfile1"); > test1 = ("passfile","pasfile1","user"); > } > > and so on .... > > The requirement is to have the file parsing so that final output is :- > > test = ("test123","abc","xyz"); > test1 = ("passfile","pasfile1","user"); > > So basically only pick the lines with maximum number of options for each > type.
The easiest solution I can think of would be to extract the first token on each line, use that token as a hash key, count the number of commas in each line, and save the line in the hash with the largest number of commas for each key. This will not work if your strings have commas. In that case, you might want to consider using a parsing module, such as Text::CSV, that will correctly handle your input data. You can use Text::CSV to split your input lines into fields and count the number of fields. However, you will first have to extract the quoted strings from the surrounding parentheses. You can use the Text::Balanced module to do that. Both Text::CSV and Text::Balanced are available at CPAN (http;//search.cpan.org). The best way for you to learn programming will be to attempt writing a program to accomplish your task, then post your program if you have trouble getting it to do what you want. Good luck. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/