Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: splitting array values

by bobn (Chaplain)
on May 20, 2004 at 04:19 UTC ( [id://354855]=note: print w/replies, xml ) Need Help??


in reply to splitting array values

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.

Replies are listed 'Best First'.
Re: splitting array values
by mkurtis (Scribe) on May 22, 2004 at 03:20 UTC
    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?
        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
        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"

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://354855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found