Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: sorting question

by kabel (Chaplain)
on Nov 21, 2002 at 06:14 UTC ( [id://214714]=note: print w/replies, xml ) Need Help??


in reply to sorting question

for short: the split accepts a regexp as first parameter, wherein the | is a metasymbol, and you did NOT quote it properly ... ;)
use strict; use warnings; use constant FIRST_FIELD => 1; use constant SECOND_FIELD => 2; use constant THIRD_FIELD => 3; my $str = ""; { local $/; $str = <DATA>; } $str = join "\n", map { $_->[0] } sort {$a->[THIRD_FIELD] cmp $b->[THIRD_FIELD] } map { [$_, split('\|', $_)] } split(/\n/, $str); print $str; __DATA__ xyz4|1026|CR xyz3|3461|CR dert5|3251|PR rtyaq|251|PR dbca|583|PR xxxt|360|CR
note further that cmp does NOT work for numerics, or look for yourself what is going wrong :) you need the <=> operator to do numeric sorting in this way.
and you saw the characters "ST" or "schwartzian transform" along with the code? ;)
HTH

UPDATE: added ST line

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-24 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found