Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Sort array

by IB2017 (Pilgrim)
on Mar 05, 2020 at 22:31 UTC ( [id://11113881]=perlquestion: print w/replies, xml ) Need Help??

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

Maybe it is the late hour, but how to sort an array like this

@arrayUnsorted=('page1.html', 'page11.html', 'page2.html', 'page3.html +');

so that I get

@arraySorted=('page1.html', 'page2.html', 'page3.html', 'page11.html') +;

I get my array reading the files inside a directory with

opendir my $dir, "." or die "Cannot open directory: $!"; my @files = readdir $dir; my @filesHTML; foreach (@files){ if ($_ =~ /html/) { push @filesHTML, $_; } }

Replies are listed 'Best First'.
Re: Sort array
by choroba (Cardinal) on Mar 05, 2020 at 22:38 UTC
    You can use a regex to extract the numbers:
    #!/usr/bin/perl use strict; use warnings; use feature "say"; my @unsorted_array = qw( page1.html page11.html page2.html page3.html +); say for sort { ($a =~ /([0-9]+)/)[0] <=> ($b =~ /([0-9]+)/)[0] } @unsorted_array;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Sort array
by AnomalousMonk (Archbishop) on Mar 05, 2020 at 23:54 UTC
Re: Sort array
by swl (Parson) on Mar 06, 2020 at 01:27 UTC
Re: Sort array
by vinoth.ree (Monsignor) on Mar 06, 2020 at 06:35 UTC
Re: Sort array
by johngg (Canon) on Mar 06, 2020 at 11:12 UTC

    A GRT sort that casts non-compliant files to the top of the sorted list.

    johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' say for map { substr $_, 54 } sort map { pack q{a50Na50}, ( m{^([A-Za-z]+)(\d+)\.html$} ? ( $1, $2 ) : + ( q{}, 0 ) ), $_ } qw{ page1.html index2.html page11.html rubbish.html page3.html inde +x1.html page2.html }' rubbish.html index1.html index2.html page1.html page2.html page3.html page11.html

    I hope this is of interest.

    Update: A scripted version that constructs a sorted array and a rejects array. This code

    use strict; use warnings; use feature qw{ say }; my @unsorted = qw{ page1.html test4.txt index2.html page11.html rubbish.html page3.html index1.html page2.html garble.html page0.html }; my @sorted; my @rejects; push @{ ( m{^\xff{54}} ) ? \ @rejects : \ @sorted }, substr $_, 54 for sort map { pack q{a50Na50}, ( m{^([A-Za-z]+)(\d+)\.html$} ? ( $1, $2 ) : ( qq{\xff} x 50, -1 ) ), $_ } @unsorted; say q{-} x 20; say for @sorted; say q{-} x 20; say for @rejects; say q{-} x 20;

    outputs

    -------------------- index1.html index2.html page0.html page1.html page2.html page3.html page11.html -------------------- garble.html rubbish.html test4.txt --------------------

    Update 2: Added {54} quantifier to regex in above code, output unchanged.

    Cheers,

    JohnGG

Re: Sort array
by hippo (Bishop) on Mar 06, 2020 at 14:45 UTC

    A solution leveraging the inferred limit of a 2-digit number in the filename (just because TIMTOWTDI).

    use strict; use warnings; use Test::More tests => 1; my @arrayUnsorted = ('page1.html', 'page11.html', 'page2.html', 'page3 +.html'); my @arraySorted = ('page1.html', 'page2.html', 'page3.html', 'page11 +.html'); my @have = sort { substr ($a, 4, 2) <=> substr ($b, 4, 2) } @arrayUnso +rted; is_deeply \@have, \@arraySorted;
Re: Sort array
by misc (Friar) on Mar 11, 2020 at 18:55 UTC
    My few cents..

    @arrayUnsorted=('page1.html', 'page11.html', 'page2.html', 'page3.html +'); my @a = sort{ ($a.$b)=~/(\d*)\.htmlpage(\d*)/;$1<=>$2; } @arrayUnsorte +d; print "$_\n" foreach @a;

    probably it might be better looking in the regex for /^page(\d*) ...
    Best wishes

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found