Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: multiple array processing

by f77coder (Beadle)
on Sep 03, 2014 at 04:44 UTC ( [id://1099342]=note: print w/replies, xml ) Need Help??


in reply to Re: multiple array processing
in thread multiple array processing

Thanks for the post.

With regards to 2. each array has different lengths, example from your post

[ ["a" .. "d"], [1 .. 9], ["p", "z", "e", "t", "i", "u"], ["foo", "bar", "baz"], ]

I would want that sorted from min to max number of elements

[ red ["foo", "bar", "baz"], ["a" .. "d"], [1 .. 9], ["p", "z", "e", "t", "i", "u"], ]

for 3., yes I want to sort the array_of_arrays by the lexical subarray variable names. maybe this needs to be shoved into a string, parsed then ordered??

a random sort of the arrays might be

[ [1 .. 9], ["foo", "bar", "baz"], ["p", "z", "e", "t", "i", "u"], ["a" .. "d"], red ]

Replies are listed 'Best First'.
Re^3: multiple array processing
by AnomalousMonk (Archbishop) on Sep 03, 2014 at 05:52 UTC

    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"], ]

      Thank you, good post.

Re^3: multiple array processing
by NetWallah (Canon) on Sep 03, 2014 at 05:16 UTC
    The structure you seem to be heading toward is something like this:
    my @AoA = ([ra_1=> \@ra_1], [ra2=>\@ra_2], [ra3=>\@ra_3], [ra4=>\@ra_ +4]); # Fat comma's used for readability
    This allows you to sort by Size, then name..
    my @AoA_sorted_by_size_Then_Name = sort {@{$a->[1]} <=> @{$b->[1]} || $a->[0] cmp $b->[0]} @AoA;
    *UNTETSED*

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      Excellent, thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found