Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Comparing Hash key with array

by InfiniteSilence (Curate)
on Feb 02, 2012 at 17:21 UTC ( #951497=note: print w/replies, xml ) Need Help??

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', '', '', ); my @dir2 = ( '_W8CXXXXAB', '2012192222', 'WHAT: HELL', 'Captain Jack', 'Wide Open', 'UNCOOL', '', ''); 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;
$VAR1 = { '_W9C2JJDCB' => [ '_W9C2JJDCB', '201200240', 'TEST: IGNORE', 'John Doe', 'Closed', 'HIP', '', '', '_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', '', '', '_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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951497]
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-05-25 09:38 GMT
Find Nodes?
    Voting Booth?