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

Hi, I'm trying to create an array of indicies got by sorting another array,
e.g if my array is @list = (4,9,3) then the array of indicies should be
(2,0,1). This question is already in the Q&A and the solution provided
by Merlyn is

my @indicies = sort { $a[$a] <=> $a[$b] } 0..$#list;
however I keep getting the message
Use of uninitialized value in numeric comparison (<=>) at test.pl
and indicies = (0,1,2), I'm rightly stumped by this and any help
greatly appreciated.
Thanks Damian
#!/usr/local/bin/perl -w @list = (4,9,3); my @indicies = sort { $a[$a] <=> $a[$b] } 0..$#list; print ("\n @indicies");

Replies are listed 'Best First'.
Re: sorting and indicies
by broquaint (Abbot) on May 23, 2002 at 14:53 UTC
    You're trying to sort on @a which doesn't exist, instead you need to sort on @list
    use strict; my @list = (4,9,3); my @indicies = sort { $list[$a] <=> $list[$b] } 0 .. $#list; print "@indicies\n";
    Note the use of strict on the first line as this is very important when learning your way around perl and beyond. For example if you'd used strict in your example you would've got an informative warning message telling you Variable "@a" is not imported and 3 error messages relating to variables not being full qualified.
    HTH

    _________
    broquaint

Re: sorting and indicies
by termix (Beadle) on May 23, 2002 at 14:56 UTC

    Your list is called "@list". This requires you to refer to its elements as $list[?] . This means that the sorting routine should be:

    my @indicies = sort { $list[$a] <=> $list[$b] ) 0.. $#list;

    If we read the line, it says:

    • @indicies is the new array
    • The sort is going to sort numbers from 0 to the highest index in "@list"
    • But the sorting comparision (the stuff in the curlies) is going to compare the values inside @list rather than the indicies themselves. However, it will reoder the indicies to create your new array.

    Much greater detail is found in  man perlfunc and also on the sort documents on this site.

    -- termix

Re: sorting and indicies
by vladb (Vicar) on May 23, 2002 at 14:53 UTC
    I guess the warning you receive is related to the fact that the @a array is empty inside the code block you pass to sort.

    I've tried to check the contents of the @a array using Data::Dumper:
    #!/usr/local/bin/perl -w use Data::Dumper; @list = (4,9,3); my @indicies = sort { print Dumper(\@a); $a[$a] <=> $a[$b] } 0..$#list +; print ("\n @indicies");
    And the output I got is this
    $VAR1 = []; Use of uninitialized value in numeric comparison (<=>) at foo.pl line +4. Use of uninitialized value in numeric comparison (<=>) at foo.pl line +4. $VAR1 = []; Use of uninitialized value in numeric comparison (<=>) at foo.pl line +4. Use of uninitialized value in numeric comparison (<=>) at foo.pl line +4. 0 1 2
    I guess you really wanted to use $list[$a] instead of, say, $a[$a]?

    UPDATE: But, hey, why did marlyn use both @a and @list? *note: check the Q&A*

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}