Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: hash of arrays of arrays

by Cristoforo (Deacon)
on Aug 23, 2012 at 23:06 UTC ( #989404=note: print w/ replies, xml ) Need Help??


in reply to hash of arrays of arrays

I believe philiprbrenan found your problem. Every time you found an entry with a new '$clean' key, you were wiping out the other entries in the hash with: %duplicates = ($clean => [$equ, $pmf, $pro, $serial, $usr, $date]);

Here is a code similiar to your problem that prints the duplicates.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %duplicates; my $serials = [ [qw/ foo bar serial1 /], [qw/ who now serial2 /], [qw/ one more serial2 /], [qw/ two end serial3 /] ]; for my $row (@$serials) { my $clean = $row->[2]; push @{$duplicates{$clean}}, $row; } print Dumper \%duplicates; for my $clean (keys %duplicates) { my $aref = $duplicates{$clean}; if (@$aref > 1) { # if duplicates for my $row (@$aref) { print "@$row\n"; } } }
Output:

C:\Old_Data\perlp>perl t7.pl $VAR1 = { 'serial3' => [ [ 'two', 'end', 'serial3' ] ], 'serial1' => [ [ 'foo', 'bar', 'serial1' ] ], 'serial2' => [ [ 'who', 'now', 'serial2' ], [ 'one', 'more', 'serial2' ] ] }; who now serial2 one more serial2
Chris

Update: Don't know why I pulled $clean out. This version doesn't.


Comment on Re: hash of arrays of arrays
Select or Download Code
Re^2: hash of arrays of arrays
by tnyflmngs (Acolyte) on Aug 24, 2012 at 02:21 UTC
    Thanks both of you. I have seen people on here talking about Data::Dumper, guess I should make the time to mess with it. I will take a look at it in the morning. I guess I thought that...who knows what I thought, it seems silly as I say it out loud. For some reason I thought that I had to be careful not to clobber my hash, but it appears that is exactly what I tried to do.
      As another suggestion, this $$xx[blah] notation is confusing. Here $row is a reference. I prefer the arrow notation. This makes it more clear that $row is a reference.
      for my $row ( @{$serials}) { my $equ = $row->[$equIndex]; my $pmf = $row->[$pmfIndex]; my $pro = $row->[$proIndex]; my $serial = $row->[$serialIndex]; my $usr = $row->[$usrIndex]; my $date = $row->[$dateIndex]; my $clean = $row->[$cleanIndex]; #blah..... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-11-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (162 votes), past polls