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

Matching strings from a file! How? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hey everyone.
I've been working with perl for awhile now, and never have I figured out how to compare a string that has been read off a file, with a non-variable string.
I put the data ripped off a file into "@INF"
if ($INF[$index] == "joe")
{
do something.
}
it seems that it's ALWAYS true, regardless whether ... it actually is or not...
using the 'ord' function, which takes the ASCII value of the character, it works.But as for strings...
It seems simple enough, but it just doesn't freaking work...
someone please help.
THANKS.
-NeX
 
You should use

if($INF[$index] eq "joe"){
whatever
}

== < > <= >= are numeric
eq gr lt ge le are for string comparison.
Good Luck!
 
You made one mistake - in Perl, the test for *numeric* equality is &quot;==&quot;, but the test for string equality is &quot;eq&quot;. Just substitute &quot;eq&quot; for &quot;==&quot; and you should be all set. HTH
---

Hardy Merrill
Mission Critical Linux, Inc.
 
hello NeXidus,
if you find a need for more powerful/flexible string comparisons, Perl has a very strong regular expression (regex) engine.

Brief example:
If you wanted to know if the array element contained the string 'joe',

if ($INF[$index] =~ /joe/)
{
print &quot;Matched 'joe'\n&quot;;
}

This gets useful fairly quickly...... maybe 'joe' is sometimes 'JOE' or 'Joe'.

you can do ......
if ($INF[$index] =~ /joe/i) { print &quot;Matched $& \n&quot;; }

It is a very short step into doing substitutions from there.... let us know if you are interested in more.

'hope this is helpful




keep the rudder amid ship and beware the odd typo
 
If you want to match exactly, though, the regex will not work as well

Say you have several Joes, joe, joe1 and joe2, the regex previously will match all of them, not just the first one. I'm not too sure how (mabye goBoating will post) you match exactly with regexs... I guess I'm used to c++, so regex's don't come to mind right away...

That's also one of Larry Wall's (The creator of perl) goals, that there's more than one way to do it!
 
mbaranski,

If you wanted to match &quot;joe&quot; in a string and not &quot;joe1&quot; or &quot;joe2&quot; then you could do this: $INF[$index] =~ /\sjoe\s/ which means that there are spaces on either side. Or if you wanted to match a string which is only &quot;joe&quot; and nothing else, then you would do $INF[$index] =~ /^joe$/ which matches the beginning and the end of the string. Or, let's say you wanted to match any word that starts with a 'j' except &quot;joe&quot;, then you could do $INF[$index] =~ /j[^oe]*\s/. Or if you wanted to match either &quot;joe&quot; or &quot;joseph&quot; then you could do $INF[$index] =~ /joe|joseph/. Or let's say you wanted to replace all instances of &quot;joe&quot; with &quot;john&quot;, then you could do $INF[$index] =~ s/joe/john/g. Et cetera. That is why regexps are better than string comparisons.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Ahhh, good explanation... One more question, though, is there any difference between

$inf[$index] =~ /^joe$/

and

$inf[$index] eq &quot;joe&quot;

Won't they both return 1, the regex b/c that's the number of matches it made, and the 2nd b/c it's true? Is there any practical difference? As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Nope, no practical difference if you want to match a single instance of an entire string, except that it's always good to get into the regexp habit just in case you want to do more than that.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top