Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Remembering original order of an array

by menolly (Hermit)
on Sep 04, 2007 at 20:33 UTC ( [id://637022]=note: print w/replies, xml ) Need Help??


in reply to Remembering original order of an array

You imply, but don't say explicitly, that all your arrays are the same size. Is this correct? Assuming it is, you have something like:

@numbers = (1,2,3,4,5,6,7); @words = ('one', 'two', 'three', 'four', 'five', 'six', 'seven'); @odd_even = ('odd', 'even', 'odd', 'even', 'odd', 'even', 'odd'); @prime = ('not', 'prime', 'prime', 'not', 'prime', 'not', 'prime');
And you want to be able to sort all of them together, based on one of them -- bringing the even numbers to the front, say?

Have you considered an array of arrays?

@numbers_with_attributes = ( [1, 'one', 'odd', 'not'], [2, 'two', 'even', 'prime'], [3, 'three', 'odd', 'prime'], [4, 'four', 'even', 'not'], [5, 'five', 'odd', 'prime'], [6, 'six', 'even', 'not'], [7, 'seven', 'odd', 'prime'], ); @alpha_sort = sort { $a[1] cmp $b[1] } @numbers_with_attributes;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://637022]
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-23 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found