Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Custom Sort Array

by 2teez (Priest)
on Aug 10, 2013 at 20:00 UTC ( #1048938=note: print w/ replies, xml ) Need Help??


in reply to Custom Sort Array

What about trying Schwartzian transform:

use strict; use warnings; print join $/ => map{$_->[0]} sort{$a->[1] <=> $b->[1]} map{[$_,/.+?-(\d+)\..+$/]}<DATA>; __DATA__ abcd1_abc_123456.abc1a_A.201307290800-0900-0.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
Produces...
abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.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


Comment on Re: Custom Sort Array
Select or Download Code
Re^2: Custom Sort Array
by omegaweaponZ (Beadle) on Aug 10, 2013 at 20:10 UTC
    Yes, I believe that is the concept I was looking for, but your example I am still only getting the same array I got before. Am I missing something simplistic? Sorry also, there is a 1, it doesn't just go 0,2,etc. There's a 1, so the 1 shows up before the 10, and 11, then the 2 shows up. This goes well beyond just 1, however. Logs can go as far as 99, so I need a solution that will organize them up to the point of this final -##.gz in the name from 0-99+ in numeric order
    foreach (@files) { my $file = $_; print join $/ => map{$_->[0]} sort{$a->[1] <=> $b->[1]} map{[$_,/.+?-(\d+)\..+$/]}$file; print "\n";

      ...There's a 1, so the 1 shows up before the 10, and 11, then the 2 shows up. This goes well beyond just 1, however. Logs can go as far as 99, so I need a solution that will organize them up to the point of this final -##.gz in the name from 0-99+ in numeric order.

      Yes, even with the 1 file been included. It would sort it for you.
      I think you are getting it wrong the way you apply the solution.
      You might want to do it like this:
      use strict; use warnings; my @files = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, /.+?-(\d+)\..+$/ ] } <DATA>; print $_,$/ for @files; __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
      you will have:
      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-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.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
        We might be looking at it differently (me being the novice!), but doing this I organize it, however, the dates will change, so it seems to only group by the last number, not by the date as well. See how all 9's and all 10's, etc, will be grouped, regardless of the prior date. That is critical or the logs will be useless
        foreach (@files) { my $file = $_; my @newfiles = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, /.+?-(\d+)\..+$/ ] } @files; print $_,$/ for @newfiles; outputs for example: abcd1_abc_123456.abc1a_A.201307282200-2300-9.gz abcd1_abc_123456.abc1a_A.201307282300-2400-9.gz abcd1_abc_123456.abc1a_A.201307272100-2200-10.gz abcd1_abc_123456.abc1a_A.201307281900-2000-10.gz I need it to output like this: abcd1_abc_123456.abc1a_A.201307282200-2300-1.gz abcd1_abc_123456.abc1a_A.201307282200-2300-2.gz abcd1_abc_123456.abc1a_A.201307282200-2300-3.gz abcd1_abc_123456.abc1a_A.201307282200-2300-4.gz ..etc.. abcd1_abc_123456.abc1a_A.201307282200-2300-10.gz abcd1_abc_123456.abc1a_A.201307282200-2300-11.gz abcd1_abc_123456.abc1a_A.201307282200-2300-12.gz abcd1_abc_123456.abc1a_A.201307282300-2400-1.gz abcd1_abc_123456.abc1a_A.201307282300-2400-2.gz abcd1_abc_123456.abc1a_A.201307282300-2400-3.gz abcd1_abc_123456.abc1a_A.201307282300-2400-4.gz abcd1_abc_123456.abc1a_A.201307282300-2400-5.gz ..etc.. abcd1_abc_123456.abc1a_A.201307282300-2400-10.gz abcd1_abc_123456.abc1a_A.201307282300-2400-11.gz abcd1_abc_123456.abc1a_A.201307282300-2400-12.gz

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-04-23 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls