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: Advanced:
How do I search for a set of characters within a variable?

 



bobjones
Deleted

Nov 24, 2000, 6:48 AM

Post #1 of 2 (865 views)
How do I search for a set of characters within a variable? Can't Post

If I had a variable $bob = "to the end"
How do I search for "end", for example, in the variable? Lets say to display $bob if "end" is found


perlplexer
Deleted

Nov 24, 2000, 7:13 AM

Post #2 of 2 (865 views)
Re: How do I search for a set of characters within a variable? [In reply to] Can't Post

1) Using Regular Expressions (perldoc perlre)

$foo = 'foo';
$bar = "foo bar bear";
print $bar if $bar =~ /$foo/;

Looks for 'foo' in $bar. Prints $bar if
'foo' is found.

2) Using index() (perldoc -f index)

$foo = 'foo';
$bar = "foo bar bear";
print $bar if index($bar, $foo)+1;

Looks for 'foo' in $bar. Prints $bar if
'foo' is found.

-------------------------
NOTE: method 2 is faster but less flexible if you want to do more complex searches.

 
 


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

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