Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can you create an array of arraynames and then print the array contents ?

Reply
Thread Tools

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

 
 
paul.porcelli@scottishwater.co.uk
Guest
Posts: n/a
 
      09-08-2005
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.

 
Reply With Quote
 
 
 
 
Miroslav Suchy
Guest
Posts: n/a
 
      09-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-08-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      09-08-2005
Miroslav Suchy <(E-Mail Removed)> 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]";
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if innerHTML used twice then it replaces the contents i want it to display all the contents virendra.amritkar@gmail.com Javascript 0 06-29-2007 08:13 AM
Can you go lens... then Circular Polarizer Fitler... then "Petal" type hood? Ryan Bygland Digital Photography 14 01-14-2007 08:32 PM
if 'ejecting' a cd is how you remove it, then when you put it in, you must be injecting a cd. Doc Martian Computer Information 4 06-09-2006 11:04 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM



Advertisments