Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Deleting from front of string

by krose (Initiate)
on Oct 05, 2001 at 02:12 UTC ( [id://116846]=perlquestion: print w/replies, xml ) Need Help??

krose has asked for the wisdom of the Perl Monks concerning the following question:

Is there a quick and simple way to remove the first two characters of a string?

Replies are listed 'Best First'.
Re: Deleting from front of string
by Moonie (Friar) on Oct 05, 2001 at 02:23 UTC
Re: Deleting from front of string
by demerphq (Chancellor) on Oct 05, 2001 at 06:21 UTC
    Frankly im amazed no one else posted this reply
    my $str='Blah blah blah'; my $removed=substr($str,0,2,""); print "Removed=$removed and Str=$str\n";
    Substr with 4 arguments sets the substr in question to equal the value of the 4th argument. In this case delete it.

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

      Also the value that substr returns is an lvalue (i.e. can be assigned to).

      perl -le '$_ = "foo bar"; substr($_,0,2) = ""; print'
Re: Deleting from front of string
by gryphon (Abbot) on Oct 05, 2001 at 02:31 UTC

    Greetings krose,

    Try the following where 2 is the number of characters you want removed from the 'some string' given:

    print substr('some string', 2);

    -gryphon
    code('Perl') || die;

Re: Deleting from front of string
by tachyon (Chancellor) on Oct 05, 2001 at 02:49 UTC
    $_ = "just another perl hacker"; $_ = reverse, chop, chop && print scalar reverse;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Ah, but what if you had some "evil" string such as:
      $_ = '10.) just another perl hacker';

      -Blake

        How about
        my $i; print join '', grep $i++>1, split //, '#!just another perl hacker';
        $_ = '10.) just another perl hacker'; $_ = reverse,chop,chop,print scalar reverse;

        Perl a problem for every solution solution for every problem

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

(Daddio) Re: Deleting from front of string
by Daddio (Chaplain) on Oct 05, 2001 at 04:10 UTC

    Other than substr, there is always the regex method:

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

    D a d d i o

      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

      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 ;-)

Re: Deleting from front of string
by ambrus (Abbot) on Apr 11, 2006 at 14:29 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-26 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found