open(HOSTDB,"hostdb") or die "cannot open hostdb"; # open hosts one time only. my @hosts; # array to hold the hosts. while(){ push @hosts, chomp($_); } # using scalar on an array will give you the number of elements print "number of records - ", scalar @hosts, "\n"; close(HOSTDB); # instead of counting and selecting the hosts, use perl's foreach # to loop inside the @hosts array foreach my $host (@hosts){ print "------host.$host--------\n"; for my $field (split /\n/, $host ) { print "Field contains: '$field'\n"; } }