Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Remembering original order of an array

by menolly (Hermit)
on Sep 04, 2007 at 20:33 UTC ( #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
Node Status?
node history
Node Type: note [id://637022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-10-21 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (217 votes). Check out past polls.

    Notices?