Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to sort in perl on the basis of one field.

by Ankur_kuls (Sexton)
on Jul 29, 2014 at 05:22 UTC ( [id://1095463]=perlquestion: print w/replies, xml ) Need Help??

Ankur_kuls has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have the below data as the elements of an array (lets say @array).

field1,fieldA,fileda (1st element) field2,fieldB,filedb (2nd element & on) field3,fieldC,filedc

where all fields are numeric values. now I need to join these elements in one scalar value ($all_data) but only after they are sorted in descending order on the basis of second field (ie fieldA, fieldB). Any one pls could help me in this...Pls let me knoe if you need more info. on that...thanks a lot

Replies are listed 'Best First'.
Re: How to sort in perl on the basis of one field.
by bigj (Monk) on Jul 29, 2014 at 05:54 UTC
    What have you tried so far?
    I won't make your homework, but I might give you some hints.

    To sort a list/array in perl, we have the sort function, to make it descending numericaly, usually, we'll have something like my @sorted_array = sort {$b <=> $a} @array or my @sorted_array = reverse sort {$a <=> $b} @array. To join something, we'll work with join usually :-) or for more flexible transformations with map. To get some fields seperated by a delimiter, we'd probably use split or in a more complex situation maybe a specialised module like Text::CSV for a CSV-file like your one looks also. So overall, you'll probably just translate your description into some perlish code 1:1 with the above built in ops/functions/modules.

    Greetings,
    Janek Schleicher

Re: How to sort in perl on the basis of one field.
by Laurent_R (Canon) on Jul 29, 2014 at 06:15 UTC
    First split your array elements in order to get an array of arrays (AoA). Something like this (untested)
    my @AoA = [split /[,\s]+/, $_] for @array;
    Then sort the AoA according to the second element. For a numerical sort, something like this (also untested):
    @AoA = sort { $b->[1] <=> $a->[1] } @AoH;
    Then you can join the elements of the inner arrays using join or map, I'll leave it to you (you don't give enough details). The whole thing could be done in one single pipeline step, but since you appear to be a beginner, it is probably better to make in in 3 steps as explained above.
Re: How to sort in perl on the basis of one field.
by 2teez (Vicar) on Jul 29, 2014 at 07:07 UTC

    Hi Aukur kuls,
    If I may add my voice to that of others, that what you seek can be done in a single "breath" with what is called popularly known as Schwartzian Transform.
    Using a modified version of what you posted one can do:

    use warnings; use strict; print map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, ( split /[,\s\D]+/, $_ )[2] ] } <DATA>; __DATA__ field15,field78,filed0 field90,field12,filed23 field3,field55,filed67

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: How to sort in perl on the basis of one field.
by Anonymous Monk on Jul 29, 2014 at 07:10 UTC
Re: How to sort in perl on the basis of one field.
by rovf (Priest) on Jul 29, 2014 at 05:52 UTC

    How are they joined? I guess you use some separator to join then. In this case, you can use (in your sort function) a regular expression to extract the second field, and sort on it.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found