Assign multiple local vars to array entries in perl -
in perl, i've been confused how cleanly assign multiple local variables array entries.
i use following syntax in subs time, i'm familiar it:
my ($var1, $var2) = @_
but other variations of confuse me. instance, have following code works:
$ctr (0 .. $#matchinglines) { $lineno = $matchinglines[$ctr][0]; $text = $matchinglines[$ctr][1];
where "@matchinglines
" array of two-element arrays.
i wish convert last 2 lines obvious:
my ($lineno, $text) = $matchinglines[$ctr];
that of course not work. i've tried numerous variations, can't find works.
it sounds have array of arrays. means inner arrays array references. if want allocate them vars need derference them.
use strict; use warnings; @matchinglines = (['y','z'],['a','b']); $ctr (0 .. $#matchinglines) { ($lineno, $text) = @{$matchinglines[$ctr]}; print "#array index: $ctr - lineno=$lineno - text=$text\n" }
this produces output
#array index: 0 - lineno=y - text=z #array index: 1 - lineno=a - text=b
Comments
Post a Comment