Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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;