Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: multiple array processing

by AnomalousMonk (Archbishop)
on Sep 03, 2014 at 03:22 UTC ( [id://1099339]=note: print w/replies, xml ) Need Help??


in reply to multiple array processing

1. sort array based on number of elements in each from smallest to largest

Here's something sort of like what I imagine you might want given the preceding array pseudo-code:

c:\@Work\Perl>perl -wMstrict -le "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, \@ra_2, \@ra_3, \@ra_4); dd \@AoA; ;; my @sorted = sort { @$a <=> @$b } @AoA; dd \@sorted; " [ ["a" .. "d"], [1 .. 9], ["p", "z", "e", "t", "i", "u"], ["foo", "bar", "baz"], ] [ ["foo", "bar", "baz"], ["a" .. "d"], ["p", "z", "e", "t", "i", "u"], [1 .. 9], ]
The statement
    my @AoA = (\@ra_1, \@ra_2, \@ra_3, \@ra_4);
can also be written
    my @AoA = \(@ra_1, @ra_2, @ra_3, @ra_4);
for the same effect; the second form is just a short-hand version of the first.

2. is it possible to sort the sub-arrays by name? then size?

I don't understand what this means. Do you want to sort by the names of the lexical variables? If so, why? Maybe you really want a more complex structure than an array or array-of-arrays, e.g, a data structure. Please see perldsc.

3. to do a random sort by name with (1A,2A…) then (1B,2B,…). ...

Any array can be randomly 'sorted'. See List::Util::shuffle.

I don't really understand the rest of the code you present in the OP.

Update: Many incremental changes. Sorry.

Replies are listed 'Best First'.
Re^2: multiple array processing
by f77coder (Beadle) on Sep 03, 2014 at 04:44 UTC

    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 ]

      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.

      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://1099339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found