Hi all i need some help with a search i am doing on
two flat text files i am looking for duplicates between the two files these two files are 60908 lines avarage, and to do a linear search is just to slow, binary search from
'Mastering Algorithms with Perl' but it doesn`t seem to
work.
here is the code i am using..
---------------Start--------------------
#!usr/bin/perl.exe
$a = 0;
open(DUP, ">Duplicates found.csv") || die $!;
open(NOR, ">Not in main.csv") || die $!;
open(FILEM, "main.csv");
open(FILET, "tmp1.csv");
@file1 = <FILEM>;
@file2 = <FILET>;
foreach $item(@file1){
$item =~ /(\d[0-9]{6,15}\d)/;
$val = &bin_search( \@file2, $1 );
#Here are some example of the first line n @file2[1]
#East Rd,GouisS Lees & PARTNERS, 12345678/1,JACOBS
#and $1 is a example of 12345678 but it doesn`t match
if(defined $val){
print DUP "$item";
}
}
close(FILEM);
close(FILET);
close(NOR);
close(DUP);
sub bin_search {
my ($file2, $word) = @_;
my ($low, $high) = ( 0, @$file2 - 1 );
while ( $low <= $high ) { # While the window is open
my $try = int( ($low+$high) /2 ); # Try the middle element
$low = $try+1, next if $array->[$try] lt $word; # Raise bottom
$high = $try-1, next if $array->[$try] gt $word; # Lower top
return $try; # We've found the word!
}
return;
}
}
--------------END---------------
ANY help with this would be great thanks a lot.
20050404 Edit by ysth: code tags; remove unneeded brs from text paragraph
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.