secondly, also always
use strict forces you to declare any new variables with my (or similar), so you'll avoid silly typos likeuse strict; use warnings;
thirdly, always quote your barewords (by default use strict forces to do this too). What I mean by this is do...my $string = "why doesn't this print?"; print $strnig;
If this seems petty, then look at the following.my $string = "A"; # rather than my $string = A;
Without knowing that A is a subroutine you could be forgiven for think your output might be A. Save yourself, and the people who maintain your code, the effort and clear up this ambiguity and always "use strict"sub A { return 'B' }; # ... 1000 lines of complicated code ... my $string = A; print "$string\n"; # this prints "B"
using warnings will give you handy hints, like when you use a variable but haven't defined a value for it yet. A lot of it's messages can be ignored, but they'll often give you hints to potential problems with your code.
Applying all this to your script gives you...
Which, when run, issues the warning "Useless use of hash element in void context at rja.pl line 12.", which might have been enough of a clue for you to figure this one out for yourself.#/perl/usr/bin use warnings; use strict; my %line; my $why = ""; my $key_a = 'A'; my $key_b = 'B'; my $key_c = 'C'; $line{$key_a}{$key_b}{$key_c}; # this is line 12 my $i = 1; for my $loop_key_a (keys %line) { for my $loop_key_b (keys %{$line{$loop_key_a}}) { for my $loop_key_c (keys %{$line{$loop_key_a}{$loop_key_b}}) { print "$loop_key_a $loop_key_b $loop_key_c\n"; } } }
Happy Coding!
In reply to Re: Hash code bug
by reasonablekeith
in thread Hash code bug
by Secode
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |