------------------------------------------------ On Wed, 23 Jul 2003 14:49:57 -0400, "West, William M" <[EMAIL PROTECTED]> wrote:
> just want to share with other unwary folk the pitfalls > of making an assumption about perl (or anything for that matter) > > You are definitely correct a hash reference is not a hash, its a reference :-). Specifically to a hash.... > > i tried for the longest time to get something like > the following to work:: > > foreach $section (keys %board){ > First indication of ill effects is that you are looping over a list and setting your control variable, but then never using it. There are plenty of times when this will be needed but it appears in this case it may have hurt you not to.... > for my $a (1..19){ > for (1..19){ > $board->{"Q1S1"}->[$a]->[$_] and print "\n$a $_"; Is the first key above where you are needing your control variable or is it something completely different? > $board->{"Q1S2"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q2S1"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q2S2"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q3S1"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q3S2"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q4S1"}->[$a]->[$_] and print "\n$a $_"; > $board->{"Q4S2"}->[$a]->[$_] and print "\n$a $_"; > } > } > } > > > well, it is supposed to repeat the inner loops several times over right? > for each hashkey, do the following.... etc etc.. > > if i got rid of the "foreach $section (keys %board){" > the rest would work fine. I was about to yell at you guys about it > when, out of desperation, i tried one last thing : > > my $board = \%board; #first line in the whole thing > > and it WORKS!!!! > Good guess, but like you said you need to know why this works... > > It turns out that a reference to a hash that's created on the 'spur of > the moment' and not explicitly made to refer to a %HASH doesn't work > the same way as a real hash :P > huh? I think I am misunderstanding what you mean, a hash reference is a hash reference is a hash reference... > now that i've figured this out, i would love to learn a bit about > /how/ this works. > Here are four very well priced places to start: perldoc perlreftut perldoc perlref perldoc perllol perldoc perldsc > $board ->{"blah"} is different from $board = \%board; $board->{"blah"} > Only in order. Assigning to and reading from a hash reference autovivified by the first line will work as it should, however it will not contain the values of any %board you had laying around at the time, that is I suppose the trap you fell into, which is why most people try to avoid using the same variable name for different types of variables despite the fact that Perl will do what you tell it happily. > i think i understand, but would like a clear explanation anyway. > Check the docs above... references is a tough subject to grasp initially but the learning curve is well worth it once the light bulb comes on. Don't get frustrated if it takes more than 15 minutes to learn.... > > btw- i know it looks like nonsense code, but i cut out some > details in the cut and paste *shrug* - just testing some functions on > an individual basis..... > > Good way to work, though for the purposes of this list it is often difficult to tell what you were doing/trying if the right amount of code is not presented. Though you seem to have grasped that concept well.... http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]