regex - Perl: Sort part of array -


i have array many fields in each line spaced different spacing like:

inddummy   drawing2   139       30        1        0        0        0        0        0 rmdummy    drawing2   69        2         1        0        0        0        0        0   pimp       drawing    7         0         1444     718      437      0        0        0 

i'm trying make sorting array number in 3rd field desired output should be:

pimp       drawing    7         0         1444     718      437      0        0        0 rmdummy    drawing2   69        2         1        0        0        0        0        0 inddummy   drawing2   139       30        1        0        0        0        0        0 

i tried make split using regular expression within sorting function like:

@sortedlistoflayers = sort {     split(m/\w+\s+(\d+)\s/gm,$a)     cmp     split(m/\w+\s+(\d+)\s/gm,$b) }@listoflayers; 

but doesn't work correctly. how make type of sorting?

you need expand out sort function little further. i'm not sure split working way think is. split turns text array based on delimiter.

i think problem regular expression - gm flags - isn't matching think it's matching. i'd perhaps approach differently though:

#!/usr/bin/perl use strict; use warnings;  @array = <data>;  sub sort_third_num {    $a1 = (split ( ' ', $a ) )[2];    $b1 = (split ( ' ', $b )) [2];    return $a1 <=> $b1; }  print sort sort_third_num @array;  __data__ nddummy   drawing2   139       30        1        0        0        0        0        0 rmdummy    drawing2   69        2         1        0        0        0        0        0   pimp       drawing    7         0         1444     718      437      0        0        0 

this trick, example.

if you're set on doing regex approach:

sub sort_third_num {     ($a1) = $a =~ m/\s(\d+)/;     ($b1) = $b =~ m/\s(\d+)/;     return $a1 <=> $b1; } 

not globally matching means first element returned. , first match of 'whitespace-digits' returned. compare numerically, rather stringwise.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -