I would like to set arrays indirectly in Perl -
i have file every 4 lines there block of information. have set 4 arrays named @linearray1, @linearray2, @linearray3, @linearray4.
i have counter gets incremented every 4 lines called $lineno.
i set 4 arrays , parse them out appropriately.
but rather set them directly 4 times in 1 line , check $lineno @ 5 rest.
so rather writing out : @linearray1=split(",",$_)
i want like: @linearray.$lineno=split(",",$_);
don't generate variable names.
instead, use
@{ $lines[$lineno] } = split /,/; or more directly:
$lines[$lineno] = [ split /,/ ]; or even
push @lines, [ split /,/ ]; you can access elements using
for $lineno (0..$#lines) { join ', ', @{ $lines[$lineno] }; } or
for $line (@lines) { join ', ', @$line; } remember use use strict; use warnings;!
Comments
Post a Comment