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

Hello there
I have a small question , if I have a a vlue like this
my $value = "1234";
how can I change it to "234" , I just want to take the first number out. so my value will be
$value = "234"
thanks for help.

janitored by ybiC: Retitle from "split value", insignificant format cleanup

Replies are listed 'Best First'.
Re: Remove leading character from string
by Roger (Parson) on Feb 02, 2004 at 01:54 UTC
    There are many ways to do this -

    1) Use substr...
    $value = substr($value, 1); # or substr($value, 0, 1) = '';

    2) Use regular expression...
    $value =~ s/^.//;

Re: Remove leading character from string
by davido (Cardinal) on Feb 02, 2004 at 06:56 UTC
    There's also the unpack method (as long as we're playing around with More Than One Way To Do It).

    my $value = "1234"; ( undef, $value ) = unpack "AA*", $value; print $value, "\n";

    Oh, and while I'm at it, here's a split method.

    my $value = "1234"; ( undef, $value ) = split //, $value, 2; print $value, "\n";

    And another split method:

    my $value = "1234"; $value = (split //, $value, 2)[1]; print $value, "\n";.

    Of course the indexed list method also works with unpack:

    my $value = "1234"; $value = (unpack "AA*", $value)[1]; print $value, "\n";

    Update: Did someone mention map? Now we're getting ridiculous:

    my $data = "1234"; my $i = 0; $data = join '', map { $i++ ? $_ : '' } split //, $data; print $data, "\n";

    Enjoy!


    Dave

Re: Remove leading character from string
by PodMaster (Abbot) on Feb 02, 2004 at 01:55 UTC
    use substr (`perldoc -f substr').

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Remove leading character from string
by NetWallah (Canon) on Feb 02, 2004 at 05:20 UTC
    Yet another way ...
    my $value=4321; my $drop_top_digit = $value % 10**(length($value) - 1); # Returns "321"

    "When you are faced with a dilemma, might as well make dilemmanade. "

Re: Remove leading character from string
by CountZero (Bishop) on Feb 02, 2004 at 07:15 UTC
    Just to show that it can be done differently and inefficiently:

    use strict; use warnings; $_="1234"; $_=join '', reverse split ''; chop; $_=join '', reverse split ''; print ;

    or

    print drop_the_first('1234'); sub drop_the_first { split '', shift; shift; return join '',@_; }

    But that last one only works in a subroutine and give a deprecation warning.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      why don't you throw in a couple of map { $_ }s in there?
      $_ = 1234; $_ = reverse $_; chop; $_ = reverse $_; print __END__ 234
      I'd do:
      sub drop_the_first { (split '', shift, 2)[1]; }
      if I was going to use split().
      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;