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

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

Comment on Removing the first character from a variable
Download Code
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-10-02 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (56 votes), past polls