in reply to multiple array processing
@array_of_arrays=(@array1A, @array1B, @array2A,@array2B…@arrayNA,@arra +yNB);
Before I say anything else, let me note that this won't work. Perl flattens lists by default, so e.g. (@array1A, @array1B) is the concatenation of the two arrays, not a two-element list containing two arrays. What you'll want is (\@array1A, \@array1B); alternatively, you can also write \(@array1A, @array1B) (personally I think this is a bit confusing, since it doesn't do what it looks like it'll do, but I'll admit it does look nicer).
So, that said...
1. sort array based on number of elements in each from smallest to largest
Easily done:
my @a1A = qw/a b c d/; my @a1B = qw/p z e t i u/; my @a2A = qw/F l i n t s t o n e/; my @a2B = qw/f o o/; my @AoA = \(@a1A, @a1B, @a2A, @a2B); my @sorted = sort { @$a <=> @$b } @AoA;
2. is it possible to sort the sub-arrays by name? then size?
I'm not sure exactly what sort order you're envisioning there, but variable names are not preserved you take (hard) references to variables, so there is no way to recover e.g. the names "array1A" and "array1B" from @array_of_arrays if you set @array_of_arrays = \(@array1A, @array1B).
That doesn't mean that what you want is impossible; you'll merely have to use a different data structure, likely something involving hashes. For instance:
my %HoA = ( "a1A" => \@a1, "a1B" => \@a1B, "a2A" => \@a2A, "a2B" => \@a2B )
Then pass an appropriate code block to sort to take into account names and array lengths, e.g. this
my @sorted = sort { substr($a, 0, 2) cmp substr($b, 0, 2) || @{ $HoA{$a} } <=> @{ $HoA{$b} } } keys %HoA; # @sorted is (a1A, a1B, a2B, a2A)
3. to do a random sort by name with (1A,2A…) then (1B,2B,…). Example
What does "random" mean? I'm guessing that you want all the "A" arrays first, then all the "B" arrays, without caring about what order they appear in; if so, use the same hash-of-arrays structure as above, and sort like this:
my @sorted = sort { substr($a, 2, 1) cmp substr($b, 2, 1) } keys %HoA; # sorted is e.g. (a2A, a1A, a1B, a2B)
If you do care about the order of each block of arrays, add an extra condition:
my @sorted = sort { substr($a, 2, 1) cmp substr($b, 2, 1) || substr($a, 1, 1) <=> substr($b, 1, 1) } keys %HoA; # sorted is (a1A, a2A, a1B, a2B)
I need the If loop rearranged based on the order of the array_of_arrays Is this possible?
Yes.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: multiple array processing
by f77coder (Beadle) on Sep 07, 2014 at 00:02 UTC | |
by AnomalousMonk (Archbishop) on Sep 07, 2014 at 00:44 UTC |