Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Help with parsing through a comma delimted file

by jlawrenc (Scribe)
on Mar 14, 2001 at 08:43 UTC ( [id://64322]=note: print w/replies, xml ) Need Help??


in reply to Help with parsing through a comma delimted file

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://64322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found