Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: substring exists at particular postion?

by hotshot (Prior)
on May 23, 2002 at 09:48 UTC ( [id://168719]=note: print w/replies, xml ) Need Help??


in reply to substring exists at particular postion?

You can use substr function for getting the substring in the positions you want and compare it to your string:
my $bigStr = 'Once upon a time there was a little programer'; my $littleStr = 'time'; my $neededPos = 12; my $length = 4; if (substr($bigStr, 12, $length) eq $neededStr) { &do_something; } else { &do_somthing_else; }


Thanks.

Hotshot

Replies are listed 'Best First'.
Re: Re: substring exists at particular postion?
by joealba (Hermit) on May 23, 2002 at 13:11 UTC
    What about this too (borrowing from hotshot's example):
    my $bigStr = 'Once upon a time there was a little programer'; my $littleStr = 'time'; if ($bigStr =~ /^(.*?)$littleStr/) { my $position = length($1); print "Found $littleStr at position $position\n"; # Do whatever depending on the position returned }
    Since you mentioned that you'd be looking for the string in more than one position, this will allow you to do the match once.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-05-28 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found