Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Custom Sort Array

by arkturuz (Curate)
on Aug 10, 2013 at 19:39 UTC ( [id://1048935]=note: print w/replies, xml ) Need Help??


in reply to Custom Sort Array

Oh, and you can declare your variable in loop. No need for my $file = $_ later:
sub by_id { # extract numbers my ($A) = $a =~ /(\d+)/; my ($B) = $b =~ /(\d+)/; # return comparison return $A <=> $B; } for my $f (sort by_id @files) { say $f; }

Replies are listed 'Best First'.
Re^2: Custom Sort Array
by 2teez (Vicar) on Aug 10, 2013 at 20:42 UTC

    Hi,
    arkturuz, will even work if the regex matches the values to be sorted like this:

    chomp(my @files = <DATA>); ## added sub by_id { # extract numbers my ($A) = $a =~ /.+?-(\d+)\..+$/; # changed my ($B) = $b =~ /.+?-(\d+)\..+$/; # changed # return comparison return $A <=> $B; } for my $f (sort by_id @files) { print $f,$/; } __DATA__ abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz

    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^2: Custom Sort Array
by omegaweaponZ (Beadle) on Aug 10, 2013 at 19:43 UTC
    Thanks, however this seems to return the same array I had previously. 10 is still preceded by 2.

    I should clarify that the logs have numbers prior as well, so the log looks something like this:

    abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz
    abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz
    abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz
    abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz
    abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz
    abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz

      You changed the requirements but I think you see where is the solution going: extract relevant fields and compare that.
Re^2: Custom Sort Array
by Anonymous Monk on Aug 12, 2013 at 16:46 UTC
    I think you meant
    my ($A) = $a =~ /(\d+)\.gz$/; my ($B) = $b =~ /(\d+)\.gz$/;

Log In?
Username:
Password:

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

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

    No recent polls found