WHY NOT FINDING HASH KEY????

J

jason

Hello.

I'm trying to merge two files by a common key field.

filea.csv format:
B1,B2,B3,B4,BK

fileb.csv format:
A1,AK,A3,A4


Need to produce:
filec.csv:
A3,A4,B2,B3,B4,BK

Where file1.csv is using BK=AK to lookup A3 and A4. Every record in
filea.csv must end up in filec.csv

Here's the code I have :



#!/usr/bin/perl
open FILEC, '>filec.csv' or die "could not open 'matched.csv' $!";
my %a;
open FILEA, 'filea.csv' "could not open 'filea' $!";
while ( <FILEA> ) {
$_ =~ s/"([^"]*)"/&comma_fixer($1)/ge;
my @f = split /,/;
my $key = $f[4];
#print $key;
if ( exists $a{$key} )
{
print "Duplicate key found in ", $key ," ",$_," ", @f,"\n";
}
else
{
$a{$key} = \@f;
}
}

open FILEB, 'fileb.csv' or die "could not open 'fileb' $!";
while ( <FILEB> ) {
my @b = split /,/;
my $key = $b[1];
#BELOW IS THE SUSPECT LINE
if ( exists $a{$key} ) {
my $m = join ',', $key,
$a{$key}[2],
$b[1],
$b[2],
$b[3];
print FILEC "$m\n";
print "entry found\n";
}
else
{
# print "key not found ",$key,"\n";
}

}

close FILEA;
close FILEB;
close FILEC;

sub comma_fixer {
$string = @_[0];
$string =~ s/,/ /g; ## replace , with blank,
return $string;


=====

Every single line spits out "key not found".
What am I doing wrong and is there a better way to approach this?
 
J

jason

My bad.
Clearly I'm desperate.

Any help or information is greatly appreciated.

Michele said:
Subject: WHY NOT FINDING HASH KEY????

Reposting with an impolite subject line is not going to help you!


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
P

Paul Lalli

My bad.
Clearly I'm desperate.

Which is massively not an excuse for rudeness. This topic is now
going on in two separate threads on clpm, and one more on
perl.beginners.
Any help or information is greatly appreciated.

Is this a joke? Is the help you've already been given in these three
threads somehow deficient?!

Paul Lalli
 
T

Tad McClellan

Subject: WHY NOT FINDING HASH KEY????


Why is your thread invisible?

Because it hits *two* scorerules that indicate it can be ignored
without missing anything of consequence:


% foolish subjects (no lower case letters)
Score: -9000
~Subject: \c[a-z]

% foolish subjects
Score:: -9000
Subject: ^perl$
Subject: ^help!?$
Subject: ^question!?$
Subject: ^perl question!?$
Subject: (none)
Subject: no subject
Subject: ^$
Subject: !!!
Subject: ###
Subject: ~~~
Subject: \?\?\?
Subject: \$\$\$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top