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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.