Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How To Read Hosts File Into a Hash

by onelesd (Pilgrim)
on Sep 15, 2011 at 23:26 UTC ( [id://926262]=note: print w/replies, xml ) Need Help??


in reply to How To Read Hosts File Into a Hash

You were close.
while (<FILE>) { next if /^\s*#/ ; # skip comments chomp; my ($key, $value) = split (/\s+/, $_); $hosts{$key} = $value; # here was your mistake }

Replies are listed 'Best First'.
Re^2: How To Read Hosts File Into a Hash
by keszler (Priest) on Sep 16, 2011 at 00:05 UTC

    Keep in mind that each line in the hosts file can define multiple aliases:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump qw/dump/; my $hostfile = "/etc/hosts"; my %hosts = (); open FILE, "<", "$hostfile" || die "Cannot open $hostfile $!"; while (<FILE>) { next if /^\s*#/ ; # skip comments chomp; my ($key, $values) = split /\s+/, $_, 2; my @values = split /\s+/, $values; $hosts{$key} = \@values; } print dump(\%hosts), $/; __END__ { "127.0.0.1" => ["localhost.localdomain", "localhost"], "127.2.3.4" => ["app.example.com"], "127.2.3.5" => ["www.example.com", "www", "web", "catalyst"], "127.2.3.6" => ["cisco.example.com", "router"], "127.2.3.7" => ["mail", "mail.example.com", "mx1"], }
Re^2: How To Read Hosts File Into a Hash
by at2marty (Novice) on Sep 16, 2011 at 00:12 UTC

    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?

      Why do I get the "uninitialized error" for $key?
      My guess is your input file has a blank line:
      while (<FILE>) { next if /^\s*#/ ; # skip comments next unless /\S/; # skip blank lines chomp; my ($key, $value) = split; $hosts{$key} = $value; }
      See perlre

      Your /etc/hosts file lines 3 and 11 have zero or more white space characters and nothing else. You should skip those:

      while (<FILE>) { next if /\A\s*\z/ ; # skip blank lines next if /^\s*#/ ; # skip comments

      Or combine the regexes:

      next if /\A\s*(?:#|\z)/ ; skip comments and blank lines

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://926262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2025-11-11 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (67 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.