×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Matching strings from a file! How?

Matching strings from a file! How?

Matching strings from a file! How?

(OP)
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

RE: Matching strings from a file! How?

You should use

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

== < > <= >= are numeric
eq gr lt ge le are for string comparison.
Good Luck!

RE: Matching strings from a file! How?

You made one mistake - in Perl, the test for *numeric* equality is "==", but the test for string equality is "eq".  Just substitute "eq" for "==" and you should be all set.

HTH
---

Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

RE: Matching strings from a file! How?

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 "Matched 'joe'\n";
}

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

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

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

RE: Matching strings from a file! How?

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!

RE: Matching strings from a file! How?

mbaranski,

If you wanted to match "joe" in a string and not "joe1" or "joe2" 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 "joe" 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 "joe", then you could do $INF[$index] =~ /j[^oe]*\s/.  Or if you wanted to match either "joe" or "joseph" then you could do $INF[$index] =~ /joe|joseph/.  Or let's say you wanted to replace all instances of "joe" with "john", 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.
http://www.oac-design.com

RE: Matching strings from a file! How?

Ahhh, good explanation...  One more question, though, is there any difference between

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

and

 $inf[$index] eq "joe"

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.

RE: Matching strings from a file! How?

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.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close