in reply to Re: declaring arrays dynamically
in thread declaring arrays dynamically

For fun,

while (@data = $sth1->fetchrow_array()) { for (my $field = 0; $field <= $#data ; $field++) { my $var_cnt = $field+1; #needed for cat in next line to work eval('push @field'.$var_cnt.', $data['.$field.'];'); } }

can be rewritten as

{ no strict 'refs'; while (@data = $sth1->fetchrow_array()) { for (my $field = 0; $field <= $#data ; $field++) { my $var_name = 'field' . ($field+1); push(@$var_name, $data[$field]); } } }

to eliminate the costly eval. You lose the ability to write to lexicals (my vars), although using those is just asking for trouble in this scenario.

A here's version (with the same caveat as the previous one) that works without (even partially) turning off strict:

my $pkg = \%::; $pkg = $pkg->{$_.'::'} foreach (split(/::/, __PACKAGE__)); while (@data = $sth1->fetchrow_array()) { for (my $field = 0; $field <= $#data ; $field++) { push(@{$pkg->{'field' . ($field+1)}}, $data[$field]); } }

Symtab manipulation is so much fun!