spickles has asked for the wisdom of the Perl Monks concerning the following question:

Why does an array hash throw out duplicate values? I have a database of objects (34 in total) and of the 7 or 8 different keys, 6 are the same with the other two being unique. When I place these keys into an array hash by querying the database and pulling from the database records it tosses out duplicates. I want to keep them.

Example input:

JP-AP1-1-RM_210,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.139 +.6,wism7-1,10.137.139.10,JP,149,not_configured JP-AP1-2-RM_207,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.139 +.6,wism7-1,10.137.139.10,JP,153,not_configured JP-AP1-3-RM_203,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.139 +.6,wism7-1,10.137.139.10,JP,157,not_configured JP-AP1-3A-RM_203,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.13 +9.6,wism7-1,10.137.139.10,JP,36,not_configured JP-AP1-4-RM_204,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.139 +.6,wism7-1,10.137.139.10,JP,161,not_configured JP-AP1-5-RM_201,Johanna Perrin,wism3-1,10.137.139.2,wism4-1,10.137.139 +.6,wism7-1,10.137.139.10,JP,149,not_configured

Example output:

JP-AP1-1-RM_210,,,,,,,,,149,not_configured, JP-AP1-10-RM_102,,,,,,,,,161,not_configured, JP-AP1-11-RM_101,,,,,,,,,149,not_configured, JP-AP1-12-RM_100,,,,,,,,,153,not_configured, JP-AP1-13-RM_Cafe_Serv_B,,,,,,,,,149,not_configured, JP-AP1-13A-RM_Cafe,,,,,,,,,36,not_configured, JP-AP1-14-RM_300,,,,,,,,,157,not_configured,

Replies are listed 'Best First'.
Re: Hash throws out duplicate values
by ikegami (Patriarch) on Oct 12, 2009 at 08:49 UTC
    If you don't want to lose records, stop overwriting them.
    $s = 123; $s = 456; print "$s\n"; # 456 $a[1] = 123; $a[1] = 456; print "$a[1]\n"; # 456 $h{foo} = 123; $h{foo} = 456; print "$h{foo}\n"; # 456
    Maybe what you want is an hash of arrays?
    push @{ $h{$k} }, 123; push @{ $h{$k} }, 456; print "$_\n" for @{ $h{$k} }; # 123, 456

      I was going to use a hash of arrays, but that will get messy real quick. So how would I use a while loop to connect to my database and populate each record of the hash? Here's my current code:

      while (my $records = $query_handle->fetchrow_hashref) { my $new_name = $records->{'ap_name'}; my $location = $records->{'building'}; my $first_name = $records->{'primary_name'}; my $first_ip = $records->{'primary_ip'}; my $second_name = $records->{'secondary_name'}; my $second_ip = $records->{'secondary_ip'}; my $third_name = $records->{'tertiary_name'}; my $third_ip = $records->{'tertiary_ip'}; my $group = $records->{'bldg_short'}; my $five_channel = $records->{'five_channel'}; my $configured = $records->{'configured'}; my $mac_name = $records->{'mac_name'}; #Add each element as a hash to an Array of Hashes (AoH) my $new_hash = { new_name => $new_name, location => $location, first_name => $first_name, first_ip => $first_ip, second_name => $second_name, second_ip => $second_ip, third_name => $third_name, third_ip => $third_ip, group => $group, five_channel => $five_channel, configured => $configured, mac_name => $mac_name }; push (@aps, $new_hash); }

      Can I step through the array 'records' and enter unique records using a loop with $i? I don't know where to place the $i to make it work.

        What's the key? Do you even need a hash? You haven't said what you're trying to do.

        I am curious, when I look at your input, I see 11 comma separated fields. When I look at your hash record field names, I see 12. Where did 'ap_name' go to or rather where could it come from? There seems to be a field missing. Perhaps I am blind and/or am miscounting. Either thing could be! I am also curious as to why you wouldn't use the same $var name as the hash key, ie, instead of my $new_name = $records->{'ap_name'}; Why not my $ap_name = $records->{'ap_name'} ?
Re: Hash throws out duplicate values
by Anonymous Monk on Oct 12, 2009 at 08:55 UTC