Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: sorting question

by kabel (Chaplain)
on Nov 21, 2002 at 06:14 UTC ( #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? ;)

UPDATE: added ST line

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://214714]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-01-16 17:38 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (186 votes). Check out past polls.