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

Hi,

I've an input that's of the format: a capital letter followed by a number e.g. 'P5' or 'P4'
I just want to get the number. What's the the best way to do it? Thanks!

Replies are listed 'Best First'.
Re: Formatting input
by davido (Cardinal) on Feb 19, 2005 at 07:50 UTC

    A few options:

    my $string = "P5"; my $num = substr $string, 1; # or my( $num ) = $string =~ m/^\w(\d+)/; # or my $num; $num = $1 if $string =~ m/^\w(\d+)/;

    ...just to name a few. :) If your input is assured to begin with a capital letter, I see no reason in particular to bother checking to see if the first character is a capital letter. For that matter, I don't really even need \w, since you already know it starts with a letter. The RE could be rewritten as m/^.(\d+)/


    Dave

      Thank you so much, Dave :) I'll use substr.