Click to See Complete Forum and Search --> : Multiple matching in FFDB with Perl


edatz
11-25-2010, 04:11 AM
Hi, I have 2 files.

I can read the first file (months.txt) and grab the number of line I want okay. From that I need to open the second one (data.txt) and show only the records which match the lines I obtained from the first one. I'm avoiding modules on this as the script it will eventually be part of does not use any.

months.txt

201011
201010
201009
201008
201007
201006
201005
201004

$mns = "2"; (201011 and 201010) - this could be more than 2
From data file get all records with these 2 in 4th field

data.txt

1|One|rest|201004
2|Two|rest|201005
3|Three|rest|201005
4|Four|rest|201010
5|Five|rest|201009
6|Six|rest|201004
7|Seven|rest|201009
8|Eight|rest|201006
9|Nine|rest|201011
10|Ten|rest|201004
11|Eleven|rest|201010
12|Twelve|rest|201010
13|Thirteen|rest|201007
14|Fourteen|rest|201011
15|Fifteen|rest|201006
16|Sixteen|rest|201008
17|Seventeen|rest|201011
18|Eighteen|rest|201008
19|Nineteen|rest|201010


This is as far as I've got. Only one of the months gets picked up though (the last one - 201010)

use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";
$mfl = "months.txt"; ## -- top down (will always be right).
$tdt = "data.txt";

$mns = "2"; # Number to show

open (MNF,"$mfl") || die("Cannot open $mfl");
@mfl = <MNF>;
close (MNF);
@lines;
for ($i = 0; $i < $mns; $i++) {
$mon = <@mfl>; push @lines, $mon;}
print "The months to show<br>"; # for test
foreach $line (@lines) {
$ctl++; # should equal $mns
$md = $line;
print "$md<br>"; # for test
}
print "showing $ctl months<br><br>"; # for test

## Now match the months with the database records and print to screen.
open (DBF,"$tdt") || die("Cannot open $tdt");
@dbm = <DBF>;
close (DBF);

print "The results<br>---------<br>"; # for test

## not sure about this kind of loop
foreach $rec (@dbm) {
@fd=split /\|/, "$rec";
$rts = "$fd[3]";

if ($rts == $md) {print "$rec<br>\n";} ## Only picks up 201010
else {}
}

What do I do to get the correct read out?

Hope someone can help please.

Thanks.

edatz
11-25-2010, 12:40 PM
I managed to do it :)
More of a guess than anything else but it works on any number I set.

open (MNF,"$mfl") || die("Cannot open $mfl");
@mfl = <MNF>;
close (MNF);
@lines;
for ($i = 0; $i < $mns; $i++) {
$mon = <@mfl>; push @lines, $mon;}
print "The months to show<br>"; # for test

open (DBF,"$tdt") || die("Cannot open $tdt");
@dbm = <DBF>;
close (DBF);
foreach $line (@lines) {
$ctl++; # should equal $mns
$md = $line;
foreach $rec (@dbm) {
@fd=split /\|/, "$rec";
$rts = "$fd[3]";
if ($rts == $md) {print "$rec<br>\n";} else {}
}
}

Returns:
9|Nine|rest|201011
14|Fourteen|rest|201011
17|Seventeen|rest|201011
4|Four|rest|201010
11|Eleven|rest|201010
12|Twelve|rest|201010
19|Nineteen|rest|201010