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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search array of patterns in another array of strings/lines 1

Status
Not open for further replies.

BondGuy

IS-IT--Management
Feb 9, 2005
10
0
0
US
Hi ,

I have a file that lists various patterns that I need to search against another file.

so basically , i was thinking it would like searching a string array in another string array.

What is tbe best possible and efficient manner of such search.

Also i am having trouble with following piece of code that I was trying to implement . any suggestions that it is not working
@name is an array that contains multiple names.
I have an html document that is being stored into @find_html
through lwp HTML-TokeParser
======================================================
while(<name>){
my $name = <name>;
@find_results = grep{/ $name/} @find_html;
print "@find_results\n";
@find_results = ();
========================================

Seems like grep is not able to get values with variable $name but if I hardcode the value , I am able to grep it.
Does grep recognize variables inside this statement.

thanks
bondguy

*learning to make my way around perl *
 
>> Does grep recognize variables inside this statement.
Yes, but you're skipping every second line in <name> and you have a newline at the end of your $name variable, which is almost definitely not what you want:
Code:
while( my $name = <name> ) {
   chomp $name;
   # now do the 'find_results' bit
}
 
Thanks.

I have to try this solution.

Meanwhile, this is what I really need help with

@name is an array with 500 names
@find_html is an array with text from an html page.

i want to find the text lines in the html page that contains those names in the page.

so basically search of an array inside another array kind of deal.

and this process has to be fast as there would be lot of html pages and the intention is to have the text information fast enough to do these searches.

any idea of what category this search is .. hash search, binary search ... ?

thanks
bondguy
 
no matter how you search through "a lot of html" documents it will probably be slow. You should consider indexing your html documents into one document and search the indexed page instead of crawling through the text of ambiguously coded html pages. But if you insist, I'd use a foreach or for loop instead of while loop:

Code:
foreach(@name){
   my @find_results = ();
   for my $i (0 .. $#find_html) {
      push(@find_results,$find_html[$i]) if (index($find_html[$i],$_) > -1);
   }
   print "@find_results";
}

using index() should be fasster than grep I would think
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top