in reply to Help with parsing through a comma delimted file
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.
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.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"; }
I hope this is helpful and maybe even a bit educational.
Jay
|
|---|