Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
P is for Practical
 
PerlMonks  

Re: Re: Re: Help with parsing through a comma delimted file

by Adam (Vicar)
on Mar 14, 2001 at 07:05 UTC ( [id://64345]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Re: Help with parsing through a comma delimted file
in thread Help with parsing through a comma delimted file

Explanation of danger's code:
#!/usr/bin/perl -w use strict; my @lines = <DATA>; # Read this file starting with the line after __D +ATA__ # Schwartzian Transform: my @sorted = map {$_->[0]} sort {$a->[1]<=>$b->[1]} map {[$_,/^(\d+)/]} @lines[2..$#lines]; # That is where all the work is done. # Read it from right to left, bottom up: # So first he uses an array slice to get get the data # minus the first two header rows. This is passed to # map(), which reads the first number in each row # and maps it to the row itself, that number is the COUNT. # This is put into an array ref # map() returns an array of those array refs, which is passed to sort( +) # sort sorts the array refs based on the second element, COUNT # and returns an array, which is passed to another map() # which extracts the first element out of the array ref # and creates the array you had in the first place, only now # it has been sorted. print @sorted; # The next line actually ends the script, and acts a marker for <DATA> __DATA__ COUNT Type Error Message ------------------------------ 3 pro bad message #1 99 dis bad message #2 209 pro bad message #3 44 dis bad message #4 19 dis Bad message #5
I suspect that uses plenty of Perl-isms, more then most people want to learn in one sitting, but You did ask how it works, and I would hate for an earnest question like that to go un-answered. I would be happy to answer any other questions you have about that code, but first you must read the docs for map, sort, perlman:perllol and of course, Efficient sorting using the Schwartzian Transform

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://64345]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.