in reply to declaring arrays dynamically

Other replies are right on the nose, you probably want an array of arrays. However, if there is some reason you can't do that (though I can't imagine why not):
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.'];'); } }
That would generate @field1, @field2,... @fieldn for the width of your row. Using an array of arrays or even a hash would work better. A hash method:
my %results; while ($data = $sth1->fetchrow_hashref) { foreach (keys %$data) { push $reults{$_}, $$data{$_}; } }
%results is now a hash of arrays. $results{degree} contains an array of all values for the field "degree" in your table. These can be extracted by:
foreach (keys %results) { print "Results for column $_:\n"; foreach (@{$results{$_}}) { print " $_\n"; } }
Or something like it.
--
$me = rand($hacker{perl});

Replies are listed 'Best First'.
Re^2: declaring arrays dynamically
by ikegami (Patriarch) on Sep 14, 2004 at 18:58 UTC

    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!