f77coder: Further to NetWallah's post, you have to realize that Perl strings are not arrays (e.g., not in the sense that C strings are char arrays). However, they do both have 'length' (number of characters vs. number of elements) and so are comparable. But when you mix strings and array references promiscuously, you must be sure each element is correctly recognized so its 'length' can be correctly derived. (This code assumes only strings and array references are mixed together, and also has a shuffle example.)
c:\@Work\Perl>perl -wMstrict -le
"use List::Util qw(shuffle);
use Data::Dump;
;;
my @ra_1 = qw(a b c d);
my @ra_2 = (1 .. 9);
my @ra_3 = qw(p z e t i u);
my @ra_4 = qw(foo bar baz);
;;
my @AoA = (\@ra_1, 'foozle', \@ra_2, 'red', \@ra_3, \@ra_4);
dd \@AoA;
;;
my @sorted =
sort { (ref $a ? @$a : length $a) <=> (ref $b ? @$b : length $b) }
@AoA
;
dd \@sorted;
;;
my @shuffled = shuffle @sorted;
dd \@shuffled;
"
[
["a" .. "d"],
"foozle",
[1 .. 9],
"red",
["p", "z", "e", "t", "i", "u"],
["foo", "bar", "baz"],
]
[
"red",
["foo", "bar", "baz"],
["a" .. "d"],
"foozle",
["p", "z", "e", "t", "i", "u"],
[1 .. 9],
]
[
[1 .. 9],
["a" .. "d"],
"foozle",
["foo", "bar", "baz"],
"red",
["p", "z", "e", "t", "i", "u"],
]
|