Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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', '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


Comment on Re: Comparing Hash key with array
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2015-07-06 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls