Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
go ahead... be a heretic
 
PerlMonks  

(Daddio) Re: Deleting from front of string

by Daddio (Chaplain)
on Oct 05, 2001 at 00:10 UTC ( [id://116870]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Deleting from front of string

Other than substr, there is always the regex method:

$_ = "just another perl hacker"; s/^.{2}(.*)$/$1/; print;

D a d d i o

Replies are listed 'Best First'.
Re: (Daddio) Re: Deleting from front of string
by hopes (Friar) on Oct 05, 2001 at 00:38 UTC
    What happens with this string?
    $_=qq!\nJ\nu\ns\t another Perl hacker!;

    Your regexp fails, because \n is not matched by /./ Nobody have said that the two first characters matches with /./
    Try this
    s/(\w|\W){2}//;
    Regards
    Hopes
      Very well spotted, Hopes. Dot not matching a newline is such a common mistake it's easy to forget. I'm glad that you noticed it.

      <pedant>

      Actually, it's possible to get dot to match everything by using the /s switch. This also has the benefit that ^ and $ will match the beginning and end of the string, and not at line breaks, which for our purposes here is desirable. See the perlre man page for details.

      Hence $string =~ s/^..//s does what we want, even if we are dealing with newlines. If you don't mind losing clarity, you can even drop the ^ in this situation.

      </pedant>

      Having said all that, the substr method described above would be my recommendation. (It's also likely to be the fastest). ;)

      Cheers,
      Paul

        I think you gave /s a few features that it doesn't posses. /s changes what '.' will match, just like you said, but if you want the '^' and '$' anchors to match around an embedded newline, you'll need to toss in the /m switch as well...

        Update: After re-reading the node I followed up to, I think we're both correct.... consider this a clarification of the node above it, rather than a correction. ;-)

        -Blake

Re: (Daddio) Re: Deleting from front of string
by cLive ;-) (Prior) on Oct 05, 2001 at 01:28 UTC
    why use $1?
    s/^.{2}//s;
    The s modifier at the end takes care of \n chars as well.

    But substr() is the best way anyway... :)

    .02

    cLive ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://116870]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.