in reply to Re: Sorting Files with Numbers
in thread Sorting Files with Numbers

I'm guessing that 'file' was just an example of a name that the orginal poster was using. It isn't very likely that the filenames will be exactly 4 chars long followed by a number. This will probably work better (untested):

sub file_sort { $a =~ /(\d*)$/; my $a_num = $1; $b =~ /(\d*)$/; my $b_num = $1; $a_num <=> $b_num; } foreach my $file (sort &file_sort keys %hash) { print "$key \n"; }

Replies are listed 'Best First'.
Re: Re: Re: Sorting Files with Numbers
by dmmiller2k (Chaplain) on Dec 31, 2002 at 15:08 UTC

    Your solution fails to sort by the alpha prefix first. See dragonchild's answer, above, which groups files first by their alpha prefix, then numerically by suffix.

    Update: jdporter's solution (here) might be even more efficient with this dataset, since it uses \D+ (non-digits) rather than \w+ (word characters) to match the prefix, but it will also match punctuation and other non-alpha characters (which may not be what you want).