in reply to Re: splitting array values
in thread splitting array values

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.

Replies are listed 'Best First'.
Re: Re: splitting array values
by duff (Parson) on May 22, 2004 at 23:34 UTC
    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?
      Thats what I though duff, but your code doesn't sort them, for some reason they stay in the same order and I don't know why.

      Thanks

      Podmaster, Heres whats happening. Ive got a scalar full of number values, some negative. It is stored in $sorted. From there the code goes like this
      foreach my $sorted(@sorted){ $scorehash{$sorted} = undef; } print Dumper(%scorehash); my @ranks2 = sort { $b <=> $a } keys %scorehash; print Dumper(\@ranks2); print $ranks2[0];
      the output is all the values not in order for both printings. Thanks

        I don't know what you are trying to do with that code... also work on your tabs. They kind of confuse the code. Espicially when that forloop is only one line.

        -- Jamie Payne
      Okay, I get these keys from the hash:
      When printed with Data Dumper they look like this:
      $VAR1 = [ '-12' ]; $VAR1 = [ '7' ]; $VAR1 = [ '26' ]; $VAR1 = [ '-123' ]; $VAR1 = [ '7' ]; $VAR1 = [ '39' ]; $VAR1 = [ '30' ]; $VAR1 = [ '32' ]; $VAR1 = [ '-504' ]; $VAR1 = [ '-12' ]; $VAR1 = [ '150' ];
      They are always in that order, even when I sort them, reverse them ...

      I think this is what you want

      Retitled by Chady -- was "Example"

Re: splitting array values
by mkurtis (Scribe) on May 22, 2004 at 22:39 UTC
    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

      Okay, when I use Data::Dumper it prints
      $VAR1 = [ '12' ];
      For all of them, the number changes but it always says $VAR1, I've never used Data::Dumper before but I don't think thats a good thing.

      Thanks