Ok. First, what you show looks like text data to perl, so I'll put it in DATA and read it like it's from a file.
I'm taking advantage of split's default arguments (split ' ', $_, where quoted space magically splits on and trims any amount of whitespace) and for's automatic loop variable, $_. The square brackets around split give me a reference to a copy of the array it produces.#!/usr/bin/perl my @multi_array; push @multi_array, [split] for <DATA>;
If you're feeling adventurous, you can replace that chunk with
uncommented.#!/usr/bin/perl # my @multi_array = map {[split]} <DATA>;
Vocation looks like it's at index 2, so we'll sort on that. Perl sort uses $a and $b as temporary storage for the two elements being compared.
The trick is that multi-dimensional arrays in perl are just ordinary arrays, with array references as elements. Hence the arrow notation. You can work that idea as deep as you need to for many dimensions.my @by_vocation = sort {$a->[2] cmp $b->[2]} @multi_array; for (@by_vocation) { print "@{$_}\n"; } __DATA__ Jeff Goldblum Actor Mary Heartman Priest John Ericsson Mathmetician Tony Cisneros Chef
After Compline,
Zaxo
In reply to Re: How to sort a multidimensional array
by Zaxo
in thread How to sort a multidimensional array
by benperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |