Reputation: 11
I am getting the following error when I try to execute my CGI script from the terminal:
Use of uninitialized value $friends{"Bob=416-333-6363"} in print at ./new-cgi/data.cgi line 24
Here is my script:
#!/usr/bin/perl -w
use strict;
my %friends;
my $name;
my $phone;
open FILE, "new-cgi/data.dat" or die ("No File\n");
while (<FILE>) {
chomp;
($name, $phone) = split(" ", $_);
$friends{$name}=$phone;
}
foreach (keys %friends) {
print "Name:", $_, "\n";
print "Phone:", $friends{$_}, "\n"; <--This is line 24
}
Upvotes: 0
Views: 142
Reputation: 3549
Hard to see without seeing your new-cgi/data.dat
file, but I assume that the data format is a bunch of lines like "Bob=416-333-6363" in which case you want to split on /=/
not " "
.
What's happening now is that you're splitting on a non-existant whitespace so $name
(the eventual key
) gets the entire line and $phone
the eventual value
, gets an undef
value. So when you iterate over the hash later, you have a hash with lots of keys (albeit with odd data for the keys) and undef
values.
Upvotes: 2