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

I realize that this should be fairly simple, but i cant figure out why its not working. I am sorting numbers this way. I tried this as an example, and it works beautifully.
my @numbers= (6, 1, -3, 7, -1290); @sorted= reverse sort { $a <=> $b } (@numbers); print @sorted;
However, i have an array full of other numbers that need to be sorted. Its scoring for a survey so that i know what surveys rank higher in terms of need. (its not for a class). The numbers were in a hash that are now put into an array. Only the (0) element of the array has them in it though. When i try to sort with just the array(0) it doesnt work.
@sorted= reverse sort { $a <=> $b } (@score[0]); print @sorted;
the above code only returns the numbers in the same order as in @score(0), which isnt in any order. I then tried splitting the numbers
@score= split(/\s/, @score[0]);
that doesnt work either, it instead puts all of the values into @score(1), which also doesnt sort when plugged into the sorter. Does anyone know why this doesnt work? thanks

mkurtis

Replies are listed 'Best First'.
Re: splitting array values
by japhy (Canon) on May 20, 2004 at 00:20 UTC
    You should show us the code that creates the @score array. Once we have that working properly, then you can just write
    @sorted = reverse sort { $a <=> $b } @score; # or @sorted = sort { $b <=> $a } @score;
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re: splitting array values
by bobn (Chaplain) on May 20, 2004 at 01:22 UTC

    First it sounds like you don't really know what is in $score[0] (note not @score[0], though that may work). To get around this,

    use Data::Dumper; print Dumper(\@score);
    This will tell you what is actually in @score, where the first element will be $score[0].

    Also, an array doesn't really contain another array. arrays can only contain scalars, though one type of scalar is a refrence, which can be thought of as a pointer to another array. If $score[0] is such a reference to an array, the correct notation is:

    @sorted= reverse sort { $a <=> $b } @{$score[0]};
    This properly de-references the referance to feed an array into the sort.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      The code that creates the array is this:
      while( ($key, $value) = each %rank ) { $rank = $rank . " " . $key; }
      Also when I print print $score[0]; it still gives me all the contents of the array. Thanks

        It looks like your making one long string in $rank, the equivalent of:$rank .= " " . $key; I'm guessing that your array has one element with one long string in it, so it looks like it's not sorted.

        If you just want to get a sorted list of the keys, you can do:

        @sorted = sort { $b <=> $a } keys %rank;
        Notice you don't have to do a reverse, you can just change the sense of the comparison.

Re: splitting array values
by bobn (Chaplain) on May 20, 2004 at 04:19 UTC
    Show us all the code startng with creation of the array and ending with the sort.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      the code that creates the array looks like this
      while( ($key, $value) = each %rank ) { $rank = $rank . " " . $key; } push @rank, split(/\s/, $rank); @rank2=sort { $b <=> $a } @rank; foreach my $rank2 (@rank2) { print $rank2 . "\n"; print "$rank2\n\n" . $rank{"$rank2"} . "\n\n"; }
      I think this is what you wanted. Thanks for the help so far.
        You seem to be doing a lot of work for something quite simple:
        my @ranks = sort { $b <=> $a } keys %rank; for my $r (@ranks) { print "$r $rank{$r}\n"; }
        Why join all of the keys together with a space, then split them apart again?
        Is there another way to sort things. For example could I take each key from the hash and compare it to all the others until I found the largest and then so on? I'm not quite sure how this would be done, but I can't get sort to work, or reverse either, it's like all the numbers are in array element and won't seperate, even though I can join them with letters in between.

        Thanks

Re: splitting array values
by dakedesu (Scribe) on Jun 01, 2004 at 03:27 UTC

    Er... where would the `cmp` operator fit into this thread? I keep seeing ascii scalar data, and the numeric '<=>' being used? I thought they behaved differently?

      If I have $\ = undef; will that have any effect on how the array is created, I just thought of this, I think that may be why it is lumping them all together.

      Sorry, Thanks for the help so far,
      mkurtis