CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Substrings

 



drSeuss
Novice

Mar 10, 2003, 9:39 AM

Post #1 of 6 (535 views)
Substrings Can't Post

Hi!

I have a question regarding strings: How can I check if one variable is "contained" in the other.



example:

$one = "Perl programming";

$two = "programmi";

$two is contained inside $one. How can I check for this?



Thanks!!!


Paul
Enthusiast

Mar 10, 2003, 10:31 AM

Post #2 of 6 (531 views)
Re: [drSeuss] Substrings [In reply to] Can't Post


Code
if (index($one, $two) > -1) { 
...
}



drSeuss
Novice

Mar 12, 2003, 11:08 AM

Post #3 of 6 (520 views)
Re: [Paul] Substrings [In reply to] Can't Post

THANKS!!!

Best wishes!

Dr. Seuss


fartface
New User

Mar 16, 2003, 4:16 PM

Post #4 of 6 (505 views)
Re: [drSeuss] Substrings [In reply to] Can't Post

Paul. I can't see how you arrived at

Code
> -1

All you need is...

Code
    if( index $one, $two ){ 
print "If it's there, well, it's there.<br>";
}

This is slightly shorter...

Code
   if( $one =~ /$two/ ){ 
print "It's there.<br>";
}



...but because you're using a more direct route, I think your idea would be more efficient.

PS. To the administrators of this site. I had to click twice to get the sign-up page (login -> Sign me up). That's 'two' many clicks! But I think it's a decent place anyway :)

PPS..sorry to whinge about this, but I've just had a whole message deleted before I posted it. I was in the process of complimenting the programmers on their thoughtful inclusion of the "Preview Post" button when it all went terribly wrong and everything just went #)(*&*(&!@#$@#$%!!. So...SORT IT OUT!


Paul
Enthusiast

Mar 16, 2003, 4:28 PM

Post #5 of 6 (502 views)
Re: [fartface] Substrings [In reply to] Can't Post

With the politest intention, RTFM :) ...your *correction* is infact incorrect and I was correct originally.

http://www.perldoc.com/perl5.8.0/pod/func/

Btw, lose the rude attitude also.


(This post was edited by Paul on Mar 16, 2003, 4:30 PM)


Jasmine
Administrator / Moderator

Mar 17, 2003, 3:45 AM

Post #6 of 6 (495 views)
Re: [fartface] Substrings [In reply to] Can't Post

Two clicks is too many? Hrm. The internet in general must be very frustrating to you Angelic The register option on the login page is very common in my experience.

What "went terribly wrong"? Can you please provide more details? You can message me with that info, if you wish.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives