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

    I can follow most of this code but what are these variables

     @$a   @$b  "special sorting variables" and  "flying saucer operator" <=>

    Is there any documentation for this more than what is here http://www.perlmeme.org/tutorials/sort_function.html??

      Here in the Monastery, see List Processing, Filtering, and Sorting in Tutorials. See perlvar for info on the  $a $b special variables (used for other things than just sorting), perlop for the spaceship  <=> and its close cousin, the  cmp operator. Also see sort for more info on this built-in function, and the sort pragma for info on controlling it. See also A Fresh Look at Efficient Perl Sorting.

      ... what are these variables

       @$a   @$b

      When sort-ing a list,  $a $b are aliased (see 'alias' in perlglossary) to various pairs of elements in the list to be sorted so that they may be compared and, if necessary, rearranged into sorted order (whatever 'sorted' means in a given context). If a list of array references is being sorted (as in some of the code examples),  $a $b become aliases of these references. If  $scalar is an array reference, then  @$scalar dereferences it. If an array reference is dereferenced in numeric context, then the number of elements of the array is returned.

      If you are sorting a list of array references by the length of each referenced array (i.e., the number of elements in each array) as in
          my @sorted = sort { @$a <=> @$b } \(@ra1, @ra2, @rax, @ray);
      @$a and  @$b in the numeric context imposed by the  <=> (spaceship) numeric comparison operator evaluate to the number of elements in their respective aliased referents.

      Update: Added  @$a <=> @$b explanation.