CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unable to compare a string from two files

 



Tejas
User

Nov 28, 2013, 12:08 AM

Post #1 of 2 (959 views)
Unable to compare a string from two files Can't Post

Below is the data from first file
872292349118
872292765118
872292968118
872294069118
872294258118
872294305118
872294570118
872294791118
872294854118
872295250118
872295425118
872295545118
872295623118
872295926118
872296181118
872296490118
872297009118
872298093118
872298602118
872298912118
872299173118
872299177118
872299229118

And below is the data from secomd file
1000012916|24-OCT-2013|14|871254308118|67.16|CAD|7|89898989
1000013530|25-OCT-2013|14|871269021118|30.28|CAD|7|89898989
1000074161|24-OCT-2013|14|871591949118|30.5|CAD|7|89898989
10000806605|24-OCT-2013|14|871702542118|31.67|CAD|7|89898989
10000833805|23-OCT-2013|14|871724740118|32.19|CAD|7|89898989
10000833805|23-OCT-2013|14|871724742118|17.3|CAD|7|89898989
1000130760|24-OCT-2013|14|871877010118|33.57|CAD|7|89898989
1000150931|25-OCT-2013|14|871842114118|32.69|CAD|7|89898989
1000153070|22-OCT-2013|14|871200079118|84.13|CAD|7|89898989
1000166106|24-OCT-2013|14|871644443118|44.61|CAD|7|89898989
1000172020|18-OCT-2013|14|869881657118|96.6|CAD|7|89898989
1000182691|21-OCT-2013|14|870905290118|30.23|CAD|7|89898989
1000200031|23-OCT-2013|14|871554316118|16.27|CAD|7|89898989
1000210510|24-OCT-2013|14|871859589118|66.45|CAD|7|89898989
1000223401|24-OCT-2013|14|871877119118|10.91|CAD|7|89898989
1000226571|24-OCT-2013|14|871896726118|132.53|CAD|7|89898989
1000234276|03-OCT-2013|14|867872650118|36.18|CAD|7|89898989
1000240286|22-OCT-2013|14|870854852118|56.44|CAD|7|89898989
1000254906|23-OCT-2013|14|871758022118|32.52|CAD|7|89898989
1000319110|22-OCT-2013|14|871495120118|60.36|CAD|7|89898989
1000319110|22-OCT-2013|14|871495121118|188.54|CAD|7|89898989
10003304805|15-OCT-2013|14|870046641118|144.38|CAD|7|89898989
1000345026|08-OCT-2013|14|868897153118|10.34|CAD|7|89898989
1000345026|10-OCT-2013|14|869220263118|31.49|CAD|7|89898989


I need to check whether the 4th value of second file matcheds with the first files's value
And i have to just save unmatched value in a new file
Below are my trials

use strict;
use warnings;
use Data::Dumper;
our %hash;
my $firstFile = $ARGV[0];
my $secondFile = $ARGV[1];
open FIRST, "< $firstFile" or die "could not open first file...\n";
while (my $line = <FIRST>) {
my @elements = split '\|', $line;
my $col3Val = $elements[3]; # Perl arrays are zero-indexed

$hash{$col3Val} = 0;

}


close FIRST;



open SECOND, "< $secondFile" or die "could not open second file...\n";
while (my $line = <SECOND>) {
my $col0ValFromSecondFile = $line;

if (defined $hash{$col0ValFromSecondFile}) {

print "$col0ValFromSecondFile Matched";
}
else {
warn "We did not locate $col0ValFromSecondFile in hash table for second file's Col2 value...\n";
}
}

close SECOND;


I suspect a big flaw in the hash ,which iam not ablt to find.
Please help me , thanks in advance

tejas


(This post was edited by Tejas on Nov 28, 2013, 1:07 AM)


BillKSmith
Veteran

Nov 28, 2013, 6:56 AM

Post #2 of 2 (953 views)
Re: [Tejas] Unable to compare a string from two files [In reply to] Can't Post

I see several minor problems. The first is that by comparing your sample data by hand, I can not find a single match.

Your text and code are not consistent about which file should be called 'first'.

There is a newline (and possibley more whitespace) at the end of $line which must be removed before the compare.

The first argument to split is a regex, not a string. Use:

Code
my @elements = split /\|/, $line;

A string will work but the special characters are different.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives