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

In reply to Re: Re: Re: Help with parsing through a comma delimted file by Adam
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.