thanks for your help. I justextracted a few lines from the file but in actuality it has no header and trust me it is comma delimted. If you get a minute could you explain what your lines do? I am sure they work but as I am new to perl an explanation would be very helpful. Txs!!!
| [reply] |
#!/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 | [reply] [d/l] |