sidsinha has asked for the wisdom of the Perl Monks concerning the following question:
A fairly simple code to load a file into a hash of keys and values. I declared my hash before the while loop globally, but when I try to use $hashofstates->($key) = $val; it throws me an error as if it wasnt declared. What could possibly be wrong?
use strict; use warnings; use Data::Dumper; my $File = "inp.txt"; open FILE, '<' , $File or die $!; my %hashofstates; while (my $line=<FILE>) { chomp $line; my ($key, $val) = split /\s+/, $line ,2; $hashofstates->($key) = $val; } close FILE or die 'Could not close file'; print (values \%hashofstates);
Error:
perl prog.pl Global symbol "$hashpfstates" requires explicit package name
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Error hash declaration
by NetWallah (Canon) on Aug 30, 2014 at 00:12 UTC | |
by sidsinha (Acolyte) on Aug 30, 2014 at 00:23 UTC | |
|
Re: Error hash declaration
by AppleFritter (Vicar) on Aug 30, 2014 at 09:37 UTC | |
|
Re: Error hash declaration
by AnomalousMonk (Archbishop) on Aug 30, 2014 at 17:12 UTC |