Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a text string in an array......

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have two lists (three actually but that is not relevant as I can repeat the process with the third file) of my customers email database. ALL PEOPLE ON THE LIST REQUESTED INFORMATION, DON'T START TELLING ME ABOUT SPAM PLEASE.

There are duplicates between the files, but each file iteself does not contain any duplicates. They are huge, 30,000 lines, one entry per line followed by a semi colon( ; )

example:

joe@joe.com;
frank@frank.net;

I just need to search one file for the contents of another line by line. If contents exists, move on, else concatenate it to the $endz.

The only thing is it does not search the array for some reason? Anyone know how to search an array for a string of text?

Note: the script is set up for testing, the final version will only write to file, not to the screen output.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

open(INITX, "file1.txt");
@info=<INITX>;
close(INITX);

open(INITY, &quot;file2.txt&quot;);
@initb=<INITY>;
close(INITY);

$a=0;
$b=0;
$c=0;
$d=0;
$endz = &quot;&quot;;

foreach (@info) {
print &quot;$info[$a]&quot;;
$a++;
}

print &quot;<hr>&quot;;
print &quot;@initb&quot;;
print &quot;<hr>&quot;;

foreach (@info) {
if (@initb =~ m/$info[$c]/) {
print &quot;<font color=red>$info[$c]</font>&quot;;
$c++;
}
else {
$endz = $endz . &quot;\n&quot; . $info[$c];
print &quot;$info[$c]&quot;;
$c++;
}
}
print &quot;<hR>&quot;;
print &quot;$endz&quot;;

I AM PRETTY SURE THE PROBLEM LIES IN if (@initb =~ m/$info[$c]/) ANY IDEAS ON HOW TO PROPERLY SEARCH AN ARRAY?
 
try

foreach $entry (@info) {
if ($entry =~ m/$info[$c]/) {

you were trying to work through the @info array on element at a time but then trying to match the whole array.. [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
You could do this:

my $infostr = join(&quot;\n&quot;,@info);

foreach $line (@initb)
{
$infostr =~ s/$line\n//g;
}

@info = split(/\n/,$infostr);

That will remove all duplicates between the two from the first one.
[sig]<p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top