Hi,
I am a bit stuck with references and was hoping someone could help me
out.
In File1 I need to isolate the last number in the column and see if
it exists in file2. If so, I would like to add the content of the
line from file1 and print it out. There may be other ways of doing
this but I wanted to try to do it with references as this is my
current weak spot :-(
Below is some sample data and my effort. What is happening with my
current attempt is that it is simply looping through the 2nd file and
printing the last assignment to the hashref. So I guess the problem
is with the way I am creating the hashref rather then dereferencing
it.
I am sure this is a simple problem. Can anyone offer a bit of
assistance . I promise will do some more reading tonight.
Thanx.
Dp.
======== my effort.pl ==========
#!/bin/perl
use strict;
use warnings;
my $file = "file1.txt";
my $file2 = "file2.txt";
my $in = 0;
my $found = 0;
my $hashref;
open(FH,$file) or die "Can't open $file:$!\n";
while (<FH>) {
chomp;
my @f = split(/\t/,);
next if ($f[3] =~ /NULL/i);
if ($f[3] =~ /(p|s)\d{4}/i){
++$in;
my $num = uc($f[3]);
my $rest = "$f[0]"."\t"."$f[1]"."\t"."$f[2]"."\t";
# print "$num\n";
$hashref = {
'number' => $num,
'rest' => $_,
};
}
}
close(FH);
open FH2, $file2 or die "Can't open $file2:$!\n";
while (<FH2>) {
chomp;
next if ($_ !~ /(P|S)\d{4}/);
(my $num) = ($_ =~ /\s(\w\d{4})/);
print "Comparing $num with $hashref->{'number'}\n";
if (exists($hashref->{'number'}) ) {
# print "$hashref->{rest} $hashref->{'number'}\n";
++$found;
}
}
close(FH2);
print "Done. Found $found / $in\n\n";
============== file1.txt ==============
42-16002862 8000014937 58000500 S8499
42-16002864 8000014938 58000501 S8500
42-16002824 8000014939 58000502 S8501
42-16002866 8000014941 58000504 S8503
42-16002868 8000014943 58000506 S8505
42-16002863 8000014944 58000507 S8506
42-16002827 8000014945 58000508 S8507
42-16002826 8000014946 58000509 S8508
42-16002823 8000014947 58000510 S8509
42-16002867 8000014948 58000511 S8510
42-16002870 8000014949 58000512 S8511
42-16002869 8000014951 58000514 S8513
42-16002871 8000014952 58000515 S8514
42-16002872 8000014953 58000516 S8515
42-16002876 8000014954 58000517 S8516
42-16002830 8000014955 58000518 S8517
42-16002829 8000014956 58000519 S8518
42-16002831 8000014957 58000520 S8519
42-16002832 8000014958 58000521 S8520
42-16002875 8000014959 58000522 S8521
42-16002834 8000014960 58000523 S8522
42-16002833 8000014961 58000524 S8523
42-16002881 8000014962 58000525 S8524
42-16002887 8000014963 58000526 S8525
42-16002884 8000014964 58000527 S8526
42-16002886 8000014965 58000528 S8527
42-16002835 8000014966 58000529 S8528
42-16002839 8000014967 58000530 S8529
42-16002838 8000014968 58000531 S8530
42-16002840 8000014969 58000532 S8531
42-16002836 8000014970 58000533 S8532
42-16002841 8000014971 58000534 S8533
42-16002883 8000014973 58000536 S8535
42-16002880 8000014974 58000537 S8536
==================================
========= file2 =====================
M175/292 S8499
P260/050 S8500
P206/202 S8501
P216/318 S8503
P216/354 S8505
P242/252 S8506
P242/207 S8507
P242/291 S8508
M175/276 S8509
M600/210 S8510
P216/282 S8511
P216/342 S8513
M560/207 S8514
P242/283 S8515
P242/238 S8516
P530/128 S8517
P242/257 S8518
P260/067 S8519
P216/346 S8520
P242/258 S8521
P206/187 S8522
P206/022 S8523
P216/307 S8524
P330/043 S8525
P256/040 S8526
M172/017 S8527
T395/070 S8528
P242/251 S8529
P248/234 S8530
P276/075 S8531
P270/060 S8532
P248/156 S8533
P276/153 S8535
P276/160 S8536
====================
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>