Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
[jedikaiti]: Hi Monks

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-04-19 17:53 GMT
Find Nodes?
    Voting Booth?