@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.


In reply to Re: multiple array processing by AppleFritter
in thread multiple array processing by f77coder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.