I love this site and have learned more about PERL here, than any other website

Not much, I guess ;^). Please, it's "Perl" (mind the capitalization).

@look = <INF>;

It's better to use <DATA> for your code. That way, the data goes with the program and you don't have to provide it separately.

@out = sort { (split '|', $a, 12)[4] <=> (split '|', $b, 12)[4] } @look;

You need to quote the pipe: '\|'.

@fields = @$line;

$line is not an arrayref. The split you did only extracted the 4th field of the string for sorting, nothing more. @out is a regular array of strings, like @look.

Applying this comments, and adding strict and warnings, your code becomes:

my @look = <DATA>; my @out = sort { (split '\|', $a, 12)[4] <=> (split '\|', $b, 12)[4] } + @look; foreach my $line (@out) { print $line; } __DATA__ accred|143|0|0|412|0|0|0|0|0|0|0|0| accu-b|36|0|0|103|0|38|0|0|0|0|2|0| accua|35|0|0|27|0|37|0|0|0|0|1|0| [...]

If you want to access the fields individually, you have to perform another split inside the foreach loop.

Further on, doing a Guttman-Rosler transform, it could be:

## tested my @out = map { substr $_, 6 } ## hardcoded "6" here... sort map { my $val = (split /\|/)[4]; sprintf "%06d$_",$val } ## ... a +nd here <DATA>;
my @d = <DATA>; my @e; my @key = map { (split /\|/)[4] } @d; use Benchmark qw/cmpthese/; cmpthese (4e4, { grt => sub { @e = map { substr $_, 6 } sort map { my $val = (sp +lit /\|/)[4]; sprintf "%06d$_",$val} @d; }, raw => sub { @e = sort { (split '\|', $a, 12)[4] <=> (split '\| +', $b, 12)[4] } @d; }, ambrus => sub { @e = @d[ sort { $key[$a] <=> $key[$b] } 0 .. @d - +1 ]; }, });
Rate raw grt ambrus raw 2742/s -- -58% -92% grt 6547/s 139% -- -80% ambrus 33058/s 1106% 405% --

Update: Added benchmark. || Included ambrus' solution.

--
David Serrano


In reply to Re: Sorting an array of arrays by field by Hue-Bond
in thread Sorting an array of arrays by field by Anonymous Monk

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.