Can you create an array of arraynames and then print the array contents ?

Discussion in 'Perl Misc' started by paul.porcelli@scottishwater.co.uk, Sep 8, 2005.

  1. Guest

    Hi there,
    suppose I create an array of arryanames
    @testarr=qw/@arr1 @arr2 @arr3/;

    The I assign values to those arrays
    @arr1=`ls /tmp`;
    @arr2=`ls /var`;

    Now I want to loop and print the first value in each array
    foreach $val (@testarr) {
    print $val[0];
    }

    The above does not work.
    Is there another way this can be done ?

    Many thanks.
    , Sep 8, 2005
    #1
    1. Advertising

  2. Re: Can you create an array of arraynames and then print the arraycontents ?

    wrote:
    > Hi there,
    > suppose I create an array of arryanames
    > @testarr=qw/@arr1 @arr2 @arr3/;
    >
    > The I assign values to those arrays
    > @arr1=`ls /tmp`;
    > @arr2=`ls /var`;
    >
    > Now I want to loop and print the first value in each array
    > foreach $val (@testarr) {
    > print $val[0];
    > }
    >
    > The above does not work.
    > Is there another way this can be done ?
    >


    Yes:


    @testarr=qw/arr1 arr2 arr3/;

    @arr1=`ls /tmp`;
    @arr2=`ls /var`;

    foreach $val (@testarr) {
    print ${$val}[0];
    }


    Miroslav Suchy
    Miroslav Suchy, Sep 8, 2005
    #2
    1. Advertising

  3. Re: Can you create an array of arraynames and then print the arraycontents ?

    wrote:
    >
    > suppose I create an array of arryanames
    > @testarr=qw/@arr1 @arr2 @arr3/;
    >
    > The I assign values to those arrays
    > @arr1=`ls /tmp`;
    > @arr2=`ls /var`;
    >
    > Now I want to loop and print the first value in each array
    > foreach $val (@testarr) {
    > print $val[0];
    > }
    >
    > The above does not work.
    > Is there another way this can be done ?


    Sure.

    my @testarr;

    for my $dir ( '/tmp', '/var' ) {
    opendir my $dh, $dir or die "Cannot open $dir: $!";
    push @testarr, [ grep !/^\./, readdir $dh ];
    }

    for my $val ( @testarr ) {
    print map "$_\n", @$val;
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2005
    #3
  4. Dave Weaver Guest

    Miroslav Suchy <> wrote:
    >
    > @testarr=qw/arr1 arr2 arr3/;
    >
    > @arr1=`ls /tmp`;
    > @arr2=`ls /var`;
    >
    > foreach $val (@testarr) {
    > print ${$val}[0];
    > }


    Symbolic references are fraught with danger.
    It's much safer to use hard references instead:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my @arr1=`ls /tmp`;
    my @arr2=`ls /var`;

    my @testarr = ( \@arr1, \@arr2 );

    foreach my $array (@testarr) {
    print $array->[0];
    }


    Or, if you need access to the names of the arrays themselves, in
    addition to the values within, use that most handy of Perl
    constructs, a hash:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my @arr1=`ls /tmp`;
    my @arr2=`ls /var`;

    my %arrays = (
    arr1 => \@arr1,
    arr2 => \@arr2,
    );

    foreach my $array_name (keys %arrays) {
    print "1st element in $array_name is $arrays{$array_name}[0]";
    }
    Dave Weaver, Sep 8, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page