Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Problem with substr

by marto (Chancellor)
on May 14, 2013 at 16:00 UTC ( #1033516=note: print w/ replies, xml ) Need Help??


in reply to Problem with substr

"e.coli.replaced becomes e.coli (which is fine)

It doesn't. With your code it becomes "e.coli.".

"but z.mays becomes z.may (which isn't)."

Don't assume what rindex returns, try this for your test case:

print rindex( $name, q{replaced} );

A simpler way of doing this would be:

my $name = "e.coli.replaced"; $name =~ s/.replaced//;

Should you actually wish the outcome to be as you specified and not what your code actually does.

Update: Fixed typo

Update 2: Dang, this is a must read, catches my mistake.


Comment on Re: Problem with substr
Select or Download Code
Re^2: Problem with substr
by newbie1991 (Acolyte) on May 14, 2013 at 16:19 UTC
    Switching to s/// did the trick. Thanks :)
Re^2: Problem with substr
by AnomalousMonk (Abbot) on May 14, 2013 at 19:34 UTC
    $name =~ s/.replaced//;

    Note that the  . (dot) in the quoted regex is the metacharacter for the operation "match any character except a newline" and as such will occasionally fail to do what newbie1991 wants. I suggest something like
        $name =~ s/\.replaced//;
    in which the dot is escaped to remove its meta-magic and make it match only a lowly period.

    >perl -wMstrict -le "my $name = 'e.coliXreplaced'; $name =~ s/.replaced//; print qq{'$name'}; " 'e.coli'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-10-21 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (100 votes), past polls