473,795 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Matching Using Scalar Variables. What am I doing wrong?

23 New Member
I've looked around online and have even had a friend help me, however, for some reason I can't compare two strings.

I'm doing this at the end of the code (within the two foreach loops), above where the 'print "found"' code is located. It works when I use a string rather than the scalar variable '$word'. However, this just isn't feasible in the program that is being developed.

If anyone sees anything that I'm doing wrong, I'd really like to know. I've tried anything that I could find online, but nothing has worked.

I didn't include all of the other components, because there was too much to it and I thought it wasn't necessary. However, if anyone need any other information, I will gladly put it up.

Thank you.


#searches the file just created for keywords
#returns the url and the keywords it found in it

#first it creates an array of lines from the web content to be searched
open (CONTENT, "spider.content .txt");
@weblist;
$k = 0;
for $weblist (<CONTENT>)
{
#file is being read and the array is being built
@weblist[$k] = $weblist;
$k++;
}
close CONTENT;

#second it builds an array of the keywords being searched for
open (KEYWORDS, "keywords.txt") ;
@keywords;
$l = 0;
for $keywords (<KEYWORDS>)
{
#file is being read and the array is being built
@keywords[$l] = $keywords;
$l++;
}
close KEYWORDS;

#third it searches that array for keywords
foreach $line (@weblist)
{
foreach $word (@keywords)
{
#fourth it prints out the url and the words found in that url
if($line =~ m/($word)/i)
{
print "found";
}
}
}
Apr 25 '07 #1
2 1832
KevinADC
4,059 Recognized Expert Specialist
Ignoring inefficiency and other problems with your code, this should work:


Expand|Select|Wrap|Line Numbers
  1. open (CONTENT, "spider.content.txt") or die "$!";
  2. @weblist = <CONTENT>;
  3. chomp(@weblist);
  4. close CONTENT;
  5.  
  6. open (KEYWORDS, "keywords.txt") or die "$!";
  7. @keywords = <KEYWORDS>;
  8. chomp(@keywords);
  9. close KEYWORDS;
  10.  
  11. foreach $line (@weblist) {
  12.    foreach $word (@keywords) {
  13.       if ($line =~ m/$word/i) {
  14.          print "found";
  15.       }
  16.    }
  17. }
if it never prints "found" we need to see some of the data to help more.
Apr 26 '07 #2
alnoir
23 New Member
Thank you. I got it to work now. I'm curious though. How could I make my code more efficient? I'm new to perl so I did what looked straight-forward. Thanks.
May 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1672
by: Leon | last post by:
Hi all. I'm using a thrid-party software library that uses two tag classes (Tag_true and Tag_false) to flag if certain operations are available in a (template) class. Now I think the idea behind them is to be able to perform compile-time checks, but I don't understand how... These tag classes are defined as empty structs, i.e.: struct Tag_true {};
2
2445
by: Yarik | last post by:
Hello there! I am working with MS SQL Server 2000. I have a table function that takes an integer parameter and returns a table, and I can successfully use it like this (passing a literal as a parameter): SELECT * FROM MyTableFunction(1)
2
6089
by: Lateralus | last post by:
headers/vector3.h: In member function ‘Vector3 Vector3::operator*(Scalar)’: headers/vector3.h:13: error: no matching function for call to ‘Vector3::Vector3(Vector3)’ ‘Vector3::Vector3(Vector3)’ headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&) I'm a little confused as to what my problem is. I get this error for any method which calls the constructor.
19
78845
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
32
14903
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
29
4325
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
6
7002
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1')); // PRINTS NOTHING print_r(strlen((int)1)); // PRINTS '1' Now I understand that in PHP, everything scalar is a string and can
16
11288
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
111
20081
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- http://www.riscossione.info/
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.