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

I'm at my wit's end, and cannot for the life of me get this to work itself out correctly! Any advice is welcome:
I have a string that's basically an ID number, consisting of a product prefix and a number, for example STPC0001. What I need to know is what number item I'm on, STPC0001 or STPC0010? Right now I'm doing the following:
if ($currNumber =~ /^($product)./) { @currNumber = split //, $currNumber; @product = split //, $product; $prodLength = @product; @adjNum = splice(@currNumber,$prodLength); }
@adjNum captures the string of numbers I want to look at - in this case it cuts off the STPC and leaves me with 0001. However, this only works for 0001-0009, when I get to 0010, it starts back at 0, and thusly screws me up. I've tried scalar(@adjNum) to kinda force the 0001 into a single number, but that also doesn't work properly after 9.

Any help/advice is *greatly* appreciated!

Replies are listed 'Best First'.
Re: Split & Splice Confusion
by HyperZonk (Friar) on Aug 17, 2001 at 23:30 UTC
    Have you considered
    $currNumber =~ /(\D*)(\d*)/
    ? This captures the prefix in $1 and the digits in $2.

    -HZ
      Yes! That works! And as a bonus, it seems to keep the leading zeros as well! I might need those in future calculations. It works great, with fewer lines!

      You've made my day! Thanks again!
Re: Split & Splice Confusion
by runrig (Abbot) on Aug 17, 2001 at 23:34 UTC
    if (my @fields = $currNumber =~ /^([^\W\d]+)(\d+)$/) { print "Product: $fields[0] Number: $fields[1]\n"; }