Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I have three arrays defined, later on th my script I have this:
my @masterarray = qw(@array1 @array2 @array3); foreach (@masterarray){ print $_; }
This will print out the name of each array, but how can I get it to print out the contents of each array? or the number of elements in each array instead?
Thanks in advance,

Jonathan

Replies are listed 'Best First'.
Re: array of arrays - context problem
by friedo (Prior) on Jan 13, 2005 at 18:03 UTC
    In order to build an array of arrays you will need to use references.

    my @masterarray = ( \@array1, \@array2, \@array3 );

    To cycle through them you can use some nested loops:

    my $c = 0; for my $a(@masterarray) { print "Array: ", ++$c, "\n"; print " $_\n" for @$a; }

    You may also want to investigate Data::Dumper. For more, see perldsc.

Re: array of arrays - context problem
by Limbic~Region (Chancellor) on Jan 13, 2005 at 18:08 UTC
    Anonymous Monk,
    As has already been indicated, the way to create a master array is my @master = (@array1, @array2, @array3); With that said, if the only reason you are doing that is so that you can loop over all 3, then the following works just fine:
    my (@array1, @array2, @array3); # defined elsewhere for ( @array1, @array2, @array3 ) { # do something with $_ }

    Cheers - L~R

Re: array of arrays - context problem
by NetWallah (Canon) on Jan 13, 2005 at 22:18 UTC
    Others have explained how to get the results you want.

    For the benefit of potentilal newbies, here is a clarification on what went wrong with the original code :

    my @masterarray = qw(@array1 @array2 @array3);
    It did NOT DWIM !

    As you have discovered, that code is equivalent to :

    my @masterarray = ('@array1','@array2','@array3');
    and this is clear in examples the documentation for qw (perldoc perlfunc).

    Although it is not explicit in the documentation, the qw operator DOES NOT INTRAPOLATE.

        ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

      Actually it is explicit in perldoc perlop in the table under the heading Quote and Quote-like Operators
Re: array of arrays - context problem
by TedYoung (Deacon) on Jan 13, 2005 at 18:01 UTC

    Update: As mentioned below, I should have mentioned some of the dangers in using symbolic references. I assumed that the example above was a simple case of what the author needed to do (for whatever reason), which is why I didn't suggest alternatives, such as arrays of arrays. They should put a warning on cold medication that suggests people should not use PerlMonks when taking this medication! :-)

    If you want to access a variable when by name, you want to use symbolic dereferencing, which is done like any other type of dereferencing:

    foreach (@masterarrray) { my @array = @$_; print int @array; # prints the length of the array print "@array"; # prints the contents # ... etc ... }

    Likewise, you can access the array without a temp variable like this:

    foreach (@masterarrray) { print int @$_; # prints the length of the array print "@$_"; # prints the contents print $$_[0]; # prints the 0th element # ... etc ... }

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

      Any post that suggests the use of symbolic references (except, maybe, dynamically inserting subroutines into a package) should have a note that says "but you don't want to do that" at the end. Followed by an explaination of a better way (which other posters have already shown).

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: array of arrays - context problem
by blazar (Canon) on Jan 14, 2005 at 16:19 UTC
    array of arrays - context problem by Anonymous Monk
    <SNIP>
    my @masterarray = qw(@array1 @array2 @array3);

    You do not have an "array of arrays" at all. C<qw//> is for defining list of "words", and it does not interpolate, as explained clearly in 'perldoc perlop'. Also, you do not have a "context problem" at all.

    This will print out the name of each array, but how can I get it to print out the contents of each array? or the number of elements in each array instead?
    If you really want to print the contents of each array by its name, then you can use symrefs, but since you do not to do that (believe me!) I'm not even mentioning how to do it. Depending on what you really want to do you may want either
    my @masterarray = (@array1, @array2, @array3);
    or
    my @masterarray = (\@array1, \@array2, \@array3);
    The latter seems more probable from your description. Then to obtain the number of elements in each of them you can try e.g.:
    my @lengths = map 0+@$_, @masterarray; # (untested!)
    You can also use scalar(), but it takes a few more keystrokes...
Re: array of arrays - context problem
by Mago (Parson) on Jan 13, 2005 at 20:35 UTC
    my @masterArray = (@array1, @array2, @array3); foreach (@masterArray){ print $_; } $numElements = @masterArray print $numElements;


    Positive vibrations;

    Mago
    mago@rio.pm.org

      Mago,
      Neither one of your comments are correct.
      • # One Element Arrays - This is just one element.
      • # Number of Elements Arrays - print takes a list (see perldoc -f print) so it actually prints the entire array. To get the number of elements use print scalar @masterArray;

      Cheers - L~R

        So thanks you;

        Copy and Paste error !

        $scalar = @array;

        or

        print scalar @array;

        or

        print int @array;



        OK !?

        Mago
        mago@rio.pm.org