You say you have verified that the hash contains all 40 elements. How? Without seeing that code it is going to be hard for us to guess why print_hash() only prints out 11 elements, but your mystery method prints out 40.
That aside, you might want to consider the following changes:
- slurping: i.e. @lines = <INFILE>. This doesn't scale well because you have to hold the entire file into memory. A better approach is to read one line in at a time:
while (my $line=<MYFILE>) {
chomp $line;
#set up hash entry
}
- two parameter open: there are some rare circumstances where this can lead to problems. Consider getting in the habit of using the 3 parameter open: open(MYFILE, '<', $infile)
- clearing @temp by setting @temp=() is unnecessary. split is going to replace the contents of @temp anyway.
- You don't actually need @temp. Perl has a wonderful feature that lets you assign array elements to a list of named variables: my ($vlan_number, $first_octet, $second_octet, $third_octet, $fourth_octet, $subnet_slash, $subnet_dotted, $network, $bldg_name, $description) = split(/,/, $line);
- Unless you are absolutely sure that there will never be whitespace on either side of your comma, split(/\s*,\s*/, $line) is a more robust way of splitting a comma delimited list.
- Assigning $hashref=\%buildings at the top, filling %buildings, and then printing $hashref seems a bit convoluted. You can assign data directly to a hash reference by using -> operator, so why not just do this:
my $buildings={}; #set up a hash reference
while (my $line = <MYFILE>) {
#... parse line
$buildings->{$bldg_name}{'name'} = $bldg_name;
$buildings->{$bldg_name}{'number'} = $vlan_number;
#... and so on
}
# ... more stuff
print_hash($buildings);
Best, beth