periapt has asked for the wisdom of the Perl Monks concerning the following question:
This got me thinking, could I use the array elt itself. The foreach construct doesn't allow a array directly, so how about a referece/dereference ... something likemy @tstary = (0,0,0,0,0); print join(":",@tstary),"\n"; foreach my $idx1 (1..3){ $tstary[1] = $idx1; print join(":",@tstary),"\n"; } __OUTPUT__ 0:0:0:0:0 0:1:0:0:0 0:2:0:0:0 0:3:0:0:0
Just a couple of thoughts on a rainy Thursday morningmy @ary03 = (0,0,0,0,0); print join(":",@ary03),"\n"; foreach ${$ary02[3]} (1..3){ $tstary[1] = $idx1; print join(":",@tstary),"\n"; } # this doesn't work with an error of # Can't use string ("0") as a symbol ref while "strict refs" in use # other variations didn't work either # how about $aryeltref = \$ary03[1]; foreach $aryeltref (1..3){ print join(":",(@tstary,$aryeltref)),"\n"; } __OUTPUT__ 0:0:0:0:0:SCALAR(0x2144a28) 0:0:0:0:0:1 0:0:0:0:0:2 0:0:0:0:0:3 # expected but not quite what I had in mind # trying foreach $$aryeltref (1..3) produced a "Not a GLOB reference a +t ... " which makes me wonder if some mojo with the symbol table migh +t make it work?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using non-scalar constructs in foreach loops
by Roy Johnson (Monsignor) on Mar 17, 2005 at 14:50 UTC | |
|
Re: Using non-scalar constructs in foreach loops
by sh1tn (Priest) on Mar 17, 2005 at 14:53 UTC | |
|
Re: Using non-scalar constructs in foreach loops
by Jasper (Chaplain) on Mar 17, 2005 at 17:47 UTC |