Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Sorting Files with Numbers

by Wonko the sane (Deacon)
on Dec 30, 2002 at 16:11 UTC ( [id://223062]=note: print w/replies, xml ) Need Help??


in reply to Sorting Files with Numbers

If all of your file names are essentially the same, except for the numeric part,
then all you should really have to do is change your comparison operator.

foreach my $file (sort {$a <=> $b} keys %hash) { print "$key \n"; }

This will work even if the Numeric part of the file is in the middle of the file name.

Ex.

perl -e 'print qq{[$_]\n} for ( sort { $a <=> $b } qw(file1 file10z fi +le11s file2q file20z file21f) )'
Wonko.

Replies are listed 'Best First'.
Re: Re: Sorting Files with Numbers
by John M. Dlugosz (Monsignor) on Dec 30, 2002 at 17:07 UTC
    No, it won't, since numberification of the string only looks at the beginning. It will see them all as zero and compare equal in every case.

    Change the order of the data in your example, since they were sorted to begin with.

    [F:\]perl -e "print qq{[$_]\n} for ( sort { print 0+$a, $b, $/; $a <=> + $b } qw(file2 file20z file22s file1q file10z file12f) )" 00 00 00 00 00 [file2] [file20z] [file22s] [file1q] [file10z] [file12f]
    —John
      Ugh, your right. Thank you for pointing this out to me.
      I like the method you used to show this.

      Wonko.

        s/your/you're/

        Thanks. I wanted to make sure there was nothing magic about the operation during a sort, compared with its normal use of simply numberifying the values. That's the problem with a language that supports DWIMary.

Log In?
Username:
Password:

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

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

    No recent polls found