Thank you for your reply. I changed my code to the following.
#!/usr/bin/perl -w
use strict;
my $hostfile = "/etc/hosts";
my %hosts = ();
open FILE, "<", "$hostfile" || die "Cannot open $hostfile $!";
while (<FILE>) {
next if /^\s*#/ ; # skip comments
chomp;
my ($key, $value) = split (" ", $_);
$hosts{$key} = $value;
}
close FILE;
The output is as follows.
Use of uninitialized value $key in hash element at hash.pl line 14, <F
+ILE> line 3.
Use of uninitialized value $key in hash element at hash.pl line 14, <F
+ILE> line 11.
Why do I get the "uninitialized error" for $key?