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

I am trying to sort an array containing positive numbers, negative numbers, and zeros using Perl's built-in sort() function. The array returned from this function drops the zeros and changes the negative numbers to positive numbers. Here is what I am trying now:
#!c:\perl\bin use strict; use warnings; my @data = (1,-5,0,-8,2,0,4,7,3,6,9); @data = sortData(@data, "Integer"); sub sortData { my @data = $_[0]..$_[scalar(@_)-2]; my $dataType = $_[scalar(@_)-1]; if($dataType eq 'Integer' || $dataType eq 'Float') { sort {$a <=> $b} @data; } else { sort @data; } }
I was expecting to get (-8, -5, 0, 0, 1, 2, 3, 4, 6, 7, 9), but instead I got (1, 2, 3, 4, 5, 6, 7, 8, 9). Any thoughts?

Replies are listed 'Best First'.
Re: A problem sorting numbers with sort()
by kvale (Monsignor) on Mar 16, 2004 at 23:22 UTC
    The problem is that you are not passing in array elements, but creating a number sequnece with .. Rewriting the sub like so
    sub sortData { my ($type, @data) = @_; return sort {$a <=> $b} @data if $type eq 'Integer' || $type eq 'Float'; return sort @data; }
    makes it easier to pass the array.

    -Mark

Re: A problem sorting numbers with sort()
by Koosemose (Pilgrim) on Mar 16, 2004 at 23:28 UTC

    Your line my @data = $_[0]..$_[scalar(@_)-2]; isn't doing what you think it is, it's passing @data a list of values using the first number in the original @data as the start of a range, and the last number as the end of a range, thereby you end up with a list of numbers from 1 to 9, probably the simplest fix would be to reverse the order you are passing the arguments in and grab them in a much simpler fashion. ie:

    #!c:\perl\bin use strict; use warnings; my @data = (1,-5,0,-8,2,0,4,7,3,6,9); @data = sortData("Integer",@data); sub sortData { my ($dataType, @data) =@_; if($dataType eq 'Integer' || $dataType eq 'Float') { sort {$a <=> $b} @data; } else { sort @data; } }

    This code will work as you expect it to, though I'm sure other monks will follow with solutions involving pops and the like so you can continue to call the sub in the same fashion...

    Just Another Perl Alchemist
Re: A problem sorting numbers with sort()
by borisz (Canon) on Mar 16, 2004 at 23:32 UTC
    As other point out there are better ways to do it. Your error is my @data = $_[0]..$_[scalar(@_)-2]; It means take the first argument from @_ and the second last one. Then fill the new array @data with 1..9 and sort that. What you want is my @data = @_[0..scalar(@_)-2];
    Boris
Re: A problem sorting numbers with sort()
by Anomynous Monk (Scribe) on Mar 17, 2004 at 04:06 UTC
    I would do this like:
    sub sortData { my $dataType = pop; if($dataType eq 'Integer' || $dataType eq 'Float') { sort {$a <=> $b} @_; } else { sort @_; } }
Re: A problem sorting numbers with sort()
by Anonymous Monk on Mar 16, 2004 at 23:57 UTC
    Problem solved.
    Thank you to everyone who responded!