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

Reference problem

Thread Next
Robert Thompson
January 5, 2002 20:19
Reference problem
Message ID:
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.


@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 ./ line 9, near "}_list"
        (Missing operator before _list?)
syntax error at ./ 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.


=-= Robert Thompson

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About