on Mon, 15 Apr 2002 13:14:40 GMT, Mok T.Y.-R15382 wrote:

> I need some help in converting strings that contain numbers back into
> numeric form. Apparently, when I sort string formatted number, the
> arrangement was according to alphanumeric order (eg. 1,
> 10,11,2,20,21,3,30... ).

Are you using the correct comparison operator? As

        perldoc -f sort

explains, the default is string-comparison (i.e. 'cmp'). If you want a 
numeric sort, you need the spaceship operator (i.e. '<=>'):

        #! /usr/local/perl -w
        use strict;

        my @strings = ('12', '1', '50', '101', '13', '501');

        my @sorted_as_strings = sort { $a cmp $b} @strings;
        my @sorted_as_numbers = sort { $a <=> $b} @strings;

        print "Original strings : @strings\n";
        print "Sorted as strings: @sorted_as_strings\n";
        print "Sorted as numbers: @sorted_as_numbers\n";

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to