use strict; use Data::Dumper; my %hosts; while () { chomp; next if /^#/; $hosts{$1} = $2 if /^(\w+)\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; warn "Garbage at line $. of DATA: $_\n" unless $1 and $2; } print Dumper(%hosts); 1; __DATA__ host1 1.13.10.116 #comment host2 21.90.30.31 #blah blah blah host3 56.87.1.10 host4 13.16.17 #The following hosts are on network B host5 57.98.18.10 #yadda yadda yadda host6 106.10.3.12