yam has asked for the wisdom of the Perl Monks concerning the following question:
So $a{1}=5.5, $a{2}=3.2, $b{1}=15.0, $b{2}=-22, $c{1}=.02 and $c{2}=.15.
Here's what I'm attempting to do:
my @fields = qw(a b c); my @input = qw( 5.5 3.2 15.0 -22 .02 .15); for ($i=0; $i<=2; $i++) { for ($j=1; $j<=2; $j++) { $${fields[$i]}{$j} = $input[((2*$i)+$j)-1]; } } for ($i=0; $i<=2; $i++) { for ($j=1; $j<=2; $j++) { print $fields[$i] . "{" . $j . "} -> " . $${fields[$i]}{$j}, "\n +"; } } printf "%.1f\t%.1f\t%.1f\n", $a{1}, $b{1}, $c{1};
The data gets entered into the hashes, though I'm not so sure now. If I run it, it prints the for loop correctly. This croaks when it gets to the printf line with a couple of "uninitialed value" warnings and prints out zeros.
Can I use the hashes by name? Do they only exist elements of the array they're in? Do I need to do my homework and look up references? I was hoping to be clever and get my way out of that (being inexperienced and all...)
Thanks for any answers
- chris
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Interpolating an array element as a hash name.
by saintmike (Vicar) on Mar 15, 2004 at 04:41 UTC | |
|
Re: Interpolating an array element as a hash name.
by Sidhekin (Priest) on Mar 15, 2004 at 04:48 UTC | |
|
Re: Interpolating an array element as a hash name.
by graff (Chancellor) on Mar 15, 2004 at 05:04 UTC | |
|
Re: Interpolating an array element as a hash name.
by TomDLux (Vicar) on Mar 15, 2004 at 05:03 UTC | |
by yam (Novice) on Mar 15, 2004 at 13:27 UTC | |
by TomDLux (Vicar) on Mar 16, 2004 at 00:28 UTC |