in reply to Interpolating an array element as a hash name.

Using the names of hashes (or of any other variables) to implement a nested data structure is almost always a bad idea.

Instead, make use of Perl's nested data structures -- they're very easy to use.

Instead of storing the name of a hash in $fields[0] and then going $${fields[$i]}{$j}, you could just as easily create a hash %hash (use a name appropriate to your application instead ;) containing the sub-hashes as values (strictly speaking, it stores references): $hash{$fields[$i]}{$j} is easier to debug because it shows the nature of the nested data structure.

With this in mind, your program looks like this:

my @fields = qw(a b c); my @input = qw( 5.5 3.2 15.0 -22 .02 .15); my %hash; for (my $i=0; $i<=2; $i++) { for (my $j=1; $j<=2; $j++) { $hash{$fields[$i]}{$j} = $input[((2*$i)+$j)-1]; } } for (my $i=0; $i<=2; $i++) { for (my $j=1; $j<=2; $j++) { print $fields[$i] . "{" . $j . "} -> " . $hash{$fields[$i]}{$j}, + "\n +"; } } printf "%.1f\t%.1f\t%.1f\n", $hash{a}{1}, $hash{b}{1}, $hash{c}{1};

--saintmike