Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

merging anonymous arrays

by SavannahLion (Pilgrim)
on Nov 16, 2012 at 08:20 UTC ( #1004146=perlquestion: print w/ replies, xml ) Need Help??
SavannahLion has asked for the wisdom of the Perl Monks concerning the following question:

In a nutshell...

my @a = qw/A B C D E F G H/; my @b = qw/7 6 5 4 3 2 1 0/; my @KEY = map {$a[$_], $b[$_]} (0 .. $#a);

How can I get rid of @a and @b by using anonymous arrays? I think I recall seeing a bit of code around here that pulls it off, or at least discusses it, but my search fu is broken.

Of worthy note:
The size of @a and @b is guaranteed to be the same size.
Can't use hashes, I need to preserve their order.
The size of each array is never more than 32ish or thereabouts.

Comment on merging anonymous arrays
Download Code
Re: merging anonymous arrays
by davido (Archbishop) on Nov 16, 2012 at 08:29 UTC

    Your existing code is but one layer of indirection away from wielding anonymous arrays:

    my $a_ref = [ qw/ A B C D E F G H / ]; my $b_ref = [ qw/ 7 6 5 4 3 2 1 0 / ]; my @KEY = map { $a_ref->[$_], $b_ref->[$_] } 0 .. $#{$a_ref};

    Similar functionality also exists in List::MoreUtils with the "zip" and "mesh" functions (they're synonymous).

    use List::MoreUtils qw( zip ); my $a_ref = [ qw/ A B C D E F G H / ]; my $b_ref = [ qw/ 7 6 5 4 3 2 1 0 / ]; my @KEY = zip @{$a_ref}, @{$b_ref};

    Dave

      Ah Ha!
      Thank you! And now I can get rid of a and b entirely by going just one step further. I knew I was overlooking something.

      Kudos.

      Or, if 'anonymous' means 'not using a name nor reference', you can change it to
      my @key = map { [qw/A B C D E F G H/]->[$_], [qw/7 6 5 4 3 2 1 0/]->[$_] } 0 .. 7;
      In such a case, you have to specify the size externally, though, since there is no way how to refer to the arrays.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        There is always a way :)
        my @key = map { [qw/A B C D E F G H/]->[$_], [qw/7 6 5 4 3 2 1 0/]->[$_] } 0..open(F,$0)&&($_=join('',<F>))&&/map\s*{\s*(\[.*?\])/s&& eval"\$#{$1}"; print "@key"; die "Not recommended for production code!!";


        When's the last time you used duct tape on a duct? --Larry Wall
        since there is no way how to refer to the arrays

        There is if you make them arguments to an on-the-fly subroutine.

        $ perl -E ' > @key = sub { > map { $_[ 0 ]->[ $_ ], $_[ 1 ]->[ $_ ] } > 0 .. $#{ $_[ 0 ] } > }->( [ qw{ A B C } ], [ qw{ 1 2 3 } ] ); > say qq{@key};' A 1 B 2 C 3 $

        A little less esoteric than ColonelPanic's clever solution ++.

        Cheers,

        JohnGG

Re: merging anonymous arrays
by Kenosis (Priest) on Nov 16, 2012 at 13:52 UTC

    And another option:

    my $a_ref = [qw/A B C D E F G H/]; my $b_ref = [qw/7 6 5 4 3 2 1 0/]; my @KEY; splice( @KEY, @KEY, 0, $a_ref->[$_], $b_ref->[$_] ) for 0 .. $#{$a_ref +};
      have a look at List::MoreUtils....good stuff for such work.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004146]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-12-28 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls