Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Removing the first character from a variable

by Kiko (Scribe)
on Jun 09, 2001 at 01:01 UTC ( #87077=perlquestion: print w/replies, xml ) Need Help??
Kiko has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a variable $ID that contains a string of data. I want to remove the first character to the left of the string. For Example
$ID=f5349j43 and i want $ID=5349j43
If it was an array i can use the shift command to remove the first thing on the left. Thanks for your help, Kiko

Replies are listed 'Best First'.
Re: Removing the first character from a variable
by wog (Curate) on Jun 09, 2001 at 01:03 UTC
(boo)Re: Removing the first character from a variable
by boo_radley (Parson) on Jun 09, 2001 at 01:14 UTC
    $foo= "string"; (undef, $foo)=split //,$foo,2; print $foo;
    $foo= "string"; ($foo)=$foo=~/.(.*)/s; print $foo;
    update changed to add /s in second example after bickering senslessly with wog.
Re: Removing the first character from a variable
by Daddio (Chaplain) on Jun 09, 2001 at 01:24 UTC


    $ID =~ s/.(.*)/$1/;

    D a d d i o

      Eh, why the (.*) and the $1? No need to say "replace the first character and the rest by the rest". "replace the first character by nothing" is far more efficient. And we can do so in two ways:
      $ID =~ s/.//s; # No s modifier needed if the first char won't be + a newline
      substr ($ID, 0, 1) = ""; # Or substr $ID, 0, 1, "";
      Of course, there are sillier ways:
      $ID = reverse $ID; chop $ID; $ID = reverse $ID;
      $ID =~ /./s; $ID = $';

      -- Abigail

      I was thinking to chop off the first character...


      and if you want to save it afterwards...

      $chopped_character = $1;

      NB: Regexps are not my strongest point, so post a reply if I am talking crap, preferably with an explanation of why!

      "Violence is the first resort of those faced with yet another BSOD."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://87077]
Approved by root
[GrandFather]: It's either lines that your browser won't split (no white space) or nodes with <pre> tags which ought to be code tags
[GrandFather]: I haven't noticed the problem for a while, but it can be hard to find the nasty node. If you do find it, consider it and with luck a janitor will fix it

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-05-24 00:58 GMT
Find Nodes?
    Voting Booth?