Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Getting the order of an array

by tall_man (Parson)
on May 23, 2005 at 21:09 UTC ( #459720=note: print w/ replies, xml ) Need Help??


in reply to Getting the order of an array

To get the OP's desired output, one needs another loop such as the one below:

use strict; my @vector = qw( 27 32 46 5 102 76 ); my @order = sort{ $vector[ $a ] <=> $vector[ $b ] } 0 .. $#vector; my $i = 1; my @order_it; foreach (@order) { $order_it[$_] = $i++; } print join(' ',@order_it),"\n"; print "@vector[ @order ]\n";

Update: Updated to show the correct way to get the second part of the OP's answer. In answer to BrowserUk's comment, I took the original question to mean that both outputs are required but that the OP didn't realize they could not both be supported by a single array.


Comment on Re: Getting the order of an array
Download Code
Re^2: Getting the order of an array
by BrowserUk (Pope) on May 23, 2005 at 22:18 UTC

    I guess it depends upon whether you feel the intermediate ordering or the final ordering specified is the crux of what the OP is trying to achieve?

    use strict; my @vector = qw( 27 32 46 5 102 76 ); my @order = sort{ $vector[ $a ] <=> $vector[ $b ] } 0 .. $#vector; my $i = 1; my @order_it; foreach (@order) { $order_it[$_] = $i++; } print join(' ',@order_it),"\n"; print "@vector[ @order_it ]\n"; __END__ P:\test>junk 2 3 4 1 6 5 Use of uninitialized value in join or string at P:\test\junk.pl line 1 +1. 46 5 102 32 76

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-09-02 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls