Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Finding location

by davidrw (Prior)
on Mar 09, 2007 at 15:12 UTC ( [id://603992]=note: print w/replies, xml ) Need Help??


in reply to Finding location

if 1-based answer, then (basically same as the above replies):
my $loc = (index($s, '*')+1) || length($s);
Just note that the strings '12*' and '123' will both yield 3 ...
If you want the latter to return '4' instead, could do:
my $loc = (index($s, '*')+1) || (length($s)+1);
Or, w/zero-base (so '12*'=>2 and '123'=>3), can do:
my $loc = ( (index($s, '*')+1) || (length($s)+1) ) - 1;
maybe not the clearest--but one-line, if that's your definition of "elegant" :)
"better" is perhaps more like (or making a sub):
my $loc = do { my $i = index $s, '*'; $i < 0 ? length($s) : $i };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found