jon36 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Sorry I am very new to perl. I looked around on the web. Would really appreciate your help. I am looking for a perl program which could handle following reqirement. Requirement: We have a pipe delimited text data file called Product_Models.txt First row of this file is headers. The rows following that will have actual data values for it. So the file Product_Models.txt will look like this: (I have kept only relevent columns here for simplicity)
ModelName|Color
model_1|Red
model_2|Blue
model_2|Green
model_2|Yellow
model_3|White
model_3|Black
model_4|Red
All I want the program to do is create a New file, which will have the only the Unique ModelName and it's Count. Column saperator will be pipe.
So for above input file, this program should create a new file called: ModelCountOutput.txt and it's content will have one header row, followed by it's values looking like this:
ModelName|Count
model_1|1
model_2|3
model_3|2
model_4|1
Thank you so much for your help!!
  • Comment on Parse a data file (.txt format), read column and have the count

Replies are listed 'Best First'.
Re: Parse a data file (.txt format), read column and have the count
by Corion (Patriarch) on Apr 26, 2011 at 21:06 UTC

    The most likely place for finding information on this problem is in your course material. Alternatively, asking your peers in your course might also help.

    If you have concrete problems, please help us to help you better and show us your code, the error it produces and please also describe what the correct output should be.

    This site is not a script writing service. We will try to help you understand your course material but neither will we read it to you nor will we do your homework.

Re: Parse a data file (.txt format), read column and have the count
by mikeraz (Friar) on Apr 26, 2011 at 21:31 UTC

    A hash can be a wonderful friend at a time like this. However hashes bestow their friendship on people who read about them in the fan magazines like perldata and perlfaq and then express the depth of their friendship with sample code.

    Will you be a friend of a hash?


    Be Appropriate && Follow Your Curiosity
Re: Parse a data file (.txt format), read column and have the count
by graff (Chancellor) on Apr 27, 2011 at 01:32 UTC
    Well, perl makes it pretty easy to handle a task like this, but it's worthwhile pointing out that standard unix/linux utilities do it easily too...
    cut -f1 -d\| input_file | sort | uniq -c
    but in order to get the particular output format you want, a little more work is needed, which is actually easiest to do with perl:
    ... | perl -pale '$_=join "|",$F[1],$F[0]'
    The "p","a","l" and "e" command-line options are described in the perlrun manual, and "join" is one of the many built-in functions described in perlfunc (and you can go directly to the description of "join" using the command line perldoc -f join.

    Please understand that "looking around the web" might not be a good strategy -- especially if you're "looking for a perl program which could handle..." some task for which you don't know the proper search term (e.g. "histogram" or "token frequency"). Look at resources that teach you how to use perl, look at existing code, play with it, break it, and learn to fix it.