Here is another approach that might be easier to understand. (The map function might be kinda heavy for newbies).

My approach will be to open the file, ignore the junk. Then for each line in the file I am going to read it, bust it apart by your criteria. In this case you've said that it is comma separated values. Now I am going to assume that you don't have field values with embedded commas to keep the code straightforward.

Once the data is busted I'm gonna save it to memory in a array of hashes structure. Each array entry will account for a line of data.

Then you want your data sorted by a field value. I am going to assume that you're sorting by count. To accomplish this we can't simply say "sort @data". We have to actually use the $datax{'count'} value for sort purposes. This is done by creating an inline sort function.

This sort/print section will expose how perl stores arrays of hashes. Each array entry is actually a reference to an anonymous hash. So the sort function is actually sorting hash references. That means when perl wants to compare two entries in the array to decide who is bigger it is comparing two hash references. ($a and $b). The inline sort function dereferences the "Count" entry of the two hash references to actually regurgiate a number which is a much more meaningful value for sorting.

Finally the value in the foreach is going to be a sorted list of hash references that are from the @data array. To actually use that data we just deference the value as I have shown below - voila data.

open FIN, "datafile.dat" || die "Cannot open datafile: $!\n"; my $line=<FIN>; $line=<FIN>; # Toss the first two lines my @data; my $index=0; while ($line=<FIN>) { chop; my @field=split /,/, $line; # Assuming that data is not # quoted or escaped - that's more work $data[$index]{'Count'}=$field[0]; $data[$index]{'Type'}=$field[1]; $data[$index]{'Message'}=$field[2]; $index++; } foreach my $data (sort {$a->{'Count'} <=> $b->{'Count'}} @data) { print "$data->{'Count'} is of type $data->{'Type'} - $data->{'Messa +ge'}\n"; }
Finally, if you actually ment to say that you have a data file of positional data - just change the @field=split line to parse your data accordingly.

I hope this is helpful and maybe even a bit educational.

Jay


In reply to Re: Help with parsing through a comma delimted file by jlawrenc
in thread Help with parsing through a comma delimted file by vonman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.