Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Sorting array

by NetWallah (Abbot)
on Jun 14, 2013 at 23:38 UTC ( #1039050=note: print w/ replies, xml ) Need Help??


in reply to Re: Sorting array
in thread Sorting array

That doesn't quite give the output the OP requested, and will fail for multi-digit "ch.." values.

If the entries always start with "ch", this code should work:

perl -e 'my @arr = ( [ ch1=> 100 ], [ ch2=> 20 ], [ ch11=> 13 ], [ ch2 +=> 45 ], [ ch1=> 1 ] ); @s= sort( { substr($arr[$a]->[0],2) <=> sub +str($arr[$b]->[0],2) or $arr[$a]->[1] <=> $arr[$b]->[1]} 0..$#arr);pr +int qq|@s\n|' #Output: #4 0 1 3 2
Note - OP's data has been modified slightly - adding a 2-digit "ch" value ("ch11").

Also - this is not the most efficient sort for this kind of data, but will work well for small to medium size data. More complex transforms are required for efficiency with large amounts of data (For some definition of 'large').

             "The trouble with the Internet is that it's replacing masturbation as a leisure activity."
        -- Patrick Murray


Comment on Re^2: Sorting array
Download Code
Re^3: Sorting array
by rkk (Novice) on Jun 14, 2013 at 23:57 UTC

    Thanks much..This is what I needed..

Re^3: Sorting array
by frozenwithjoy (Curate) on Jun 15, 2013 at 00:34 UTC
    I thought about that; however, all current genome annotation (that I've seen) has 0-padded #s such that if there are > 9 chromosomes, the first 9 would be ch01, ch02, etc. In the 'real world', it is much more likely to have zero-padding than to have chromosomes names 'ch'. But I agree, better safe than sorry.
Re^3: Sorting array
by rkk (Novice) on Jun 15, 2013 at 02:48 UTC

    I am getting some error..BTW I have created the above mentioned @arr using following command

    push(@arr,($l2[6],$l2[7]));

    But you you specified

    my @arr = ( [ ch1=> 100 ], [ ch2=> 20 ], [ ch11=> 13 ], [ ch2 => 45 ], + [ ch1=> 1 ] );

    please advise changes in my command to get a format like you specified. Thanks in advance..

      I figured it out.. I need to use [] instead of ()..

      Never mind..

      Try:
      push @arr, [ $l2[6], $l2[7] ];
      See " perldoc perllol" to understand how this works.

                   "The trouble with the Internet is that it's replacing masturbation as a leisure activity."
              -- Patrick Murray

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-18 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls