develooper Front page | perl.beginners | Postings from January 2002

Reference problem

Thread Next
From:
Robert Thompson
Date:
January 5, 2002 20:19
Subject:
Reference problem
Message ID:
20020106041904.A87585@thorshammer.org
Hello list,

	I am attempting to use some references for variable names in some loops, and am having problems. Below is some test code for what I am attempting to accomplish.

#!/usr/bin/perl

@one_list = qw(1 2 3 4);
@two_list = qw(5 6 7 8);
@three_list = qw(9 10 11 12);
@four_list = qw(13 14 15 16);

foreach my $array (qw(one two three four)) {
  foreach my $num ( @{$array}_list ) {   <== Break here
    print $array . "_" . $num . "\n";
  }
}


	The error I am getting is that the "_list" is not seen as part of the variable name in the foreach loop.

Bareword found where operator expected at ./test.pl line 9, near "}_list"
        (Missing operator before _list?)
syntax error at ./test.pl line 9, near "}_list"

	I have tried all combinations of brackets and text I can think of, and putting the extra text in the begining of the variable, with no success.

	I need the output to be in the form "one_1", "one_2", "two_8", "three_11", etc. I can just write a different foreach loop for each array, but that seems redundant.

Thanks,

=-= Robert Thompson

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About