Just use an ordinary for loop to step through 3 elements at a time:
for (my $i=0; $i<$#dynval3; $i+=3) { my ( $a, $b, $c ) = @dynval3[$i..$i+2]; # shorthand slice idiom i +nstead of $ary[$i], $ary[$i+1],... print "$a|$b|$c\n"; }
But if you are creating this data structure an easier structure would be an array of arrays which is perls version of a list of lists.
my @dynval3 = ( [ 'Version', 'obVersion', '0x0204' ], [ 'Stat', 'obStat', '0x04b4' ], [ 'Mode', 'obMode', '0x0020' ], [ 'Regulation', 'obRegulation', '0x0014' ], [ 'Scheduler', 'obScheduler', '0x0001' ], ); for my $ref(@dynval3) { # access examples print "@$ref\n"; my ( $a, $b, $c ) = @$ref; print "$a $b $c\n"; print "Field 1 is: ", $ref->[1], "\n\n"; }
See perlman:perlref
cheers
tachyon
In reply to Re: loop thru 3-element array?
by tachyon
in thread loop thru 3-element array?
by anadem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |