http://www.perlmonks.org?node_id=87077

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
    Timtowdi.
    $foo= "string"; (undef, $foo)=split //,$foo,2; print $foo;
    or
    $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

    AYAWTDI

    $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
      or
      substr ($ID, 0, 1) = ""; # Or substr $ID, 0, 1, "";
      Of course, there are sillier ways:
      $ID = reverse $ID; chop $ID; $ID = reverse $ID;
      or
      $ID =~ /./s; $ID = $';

      -- Abigail

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

      $id=~s/^(.)//;

      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."
      --/me