http://www.perlmonks.org?node_id=951497


in reply to Comparing Hash key with array

Anonymous Monk already pointed out that you are comparing strings using == incorrectly. That being said, I just found the way you are doing this near incomprehensible. The code is verbose and that confounds readability. I refactored:

use strict; use Data::Dumper; my @line1 = ('_W9C2JJDCB','<P>This is the problem1.</P>','<P>This is r +es1</P>'); my @line2 = ('_W9C2JJDCB','<P>This is the problem2.</P>','<P>This is r +es2</P>'); my @line3 = ('_W9C2JJDCB','<P>This is the problem3.</P>','<P>This is r +es3</P>'); my @line4 = ('_W8CXXXXAB','<p>hi there</p>','<p>why are you capitalizi +ng paragraph tags?</p>'); my @totlines; #push (@totlines,\@line1); #push (@totlines,\@line2); #push (@totlines,\@line3); push @totlines, \@line1, \@line2,\@line3,\@line4; my @dir1 =('_W9C2JJDCB', '201200240', 'TEST: IGNORE', 'John Doe', 'Closed', 'HIP', 'email@email.com', 'email2@email.com', ); my @dir2 = ( '_W8CXXXXAB', '2012192222', 'WHAT: HELL', 'Captain Jack', 'Wide Open', 'UNCOOL', 'you@notme.com', 'stillyou@notme.com'); my %hash = ( '_W9C2JJDCB' => \@dir1); $hash{'_W8CXXXXAB'} = \@dir2; for my $key (keys %hash){ foreach my $u (@totlines) { if($key eq $u->[0]) { #print qq|$key is equal to $u->[0]\n|; push @{$hash{$key}},@$u; } } } print Dumper \%hash;
Makes:
$VAR1 = { '_W9C2JJDCB' => [ '_W9C2JJDCB', '201200240', 'TEST: IGNORE', 'John Doe', 'Closed', 'HIP', 'email@email.com', 'email2@email.com', '_W9C2JJDCB', '<P>This is the problem1.</P>', '<P>This is res1</P>', '_W9C2JJDCB', '<P>This is the problem2.</P>', '<P>This is res2</P>', '_W9C2JJDCB', '<P>This is the problem3.</P>', '<P>This is res3</P>' ], '_W8CXXXXAB' => [ '_W8CXXXXAB', '2012192222', 'WHAT: HELL', 'Captain Jack', 'Wide Open', 'UNCOOL', 'you@notme.com', 'stillyou@notme.com', '_W8CXXXXAB', '<p>hi there</p>', '<p>why are you capitalizing paragraph tag +s?</p>' ] };
I think this is what you were trying to do. I copied your @$u from the original code but as you can see from the output that copies the id over and over again. You could use a slice like this instead:
perl -e '@a = 1..5; print @a[1..@a];'

Celebrate Intellectual Diversity