Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: splitting array values

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


in reply to splitting array values

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.

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

        I tried sorting the keys like you said VSarkiss, but it still doesn't change the order when I sort them, or when I reverse them either.

        thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found