in reply to Re^3: Populate Hash
in thread Populate Hash
[/tmp]$ cat first.pl #!/usr/bin/perl use warnings; use strict; %myHash=(); sub populateHash { my $MY_FILE="file.properties"; open my $PFILE, '<', $MY_FILE or die "Cannot open '$MY_FILE' $ +!"; LINE: while (<$PFILE>) { # now read the line and remove all white space +s, tab and new line chars or any trailing spaces $_ =~ s/\s+$//; next LINE if (/^\s*#/); # ignore commented line +s next LINE if (/^$/); # ignore null lines my $n=substr($_,0,index($_,'=')); my $v=substr($_,index($_,'=')+1); $myHash->{$n}=$v; } close $PFILE; } &populateHash(); while ( my ($key, $value) = each(%myHash) ) { print "From myHash --> $key => $value\n"; } The OUTPUT is: ================ [/tmp]$ perl first.pl Global symbol "%myHash" requires explicit package name at first.pl lin +e 5. Global symbol "$myHash" requires explicit package name at first.pl lin +e 22. Global symbol "%myHash" requires explicit package name at first.pl lin +e 31. Execution of first.pl aborted due to compilation errors.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Populate Hash
by Corion (Patriarch) on Jul 27, 2009 at 14:25 UTC | |
| |
|
Re^5: Populate Hash
by mzedeler (Pilgrim) on Jul 27, 2009 at 14:25 UTC |