Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

incrementing a number

by mmittiga17 (Scribe)
on Oct 12, 2010 at 16:15 UTC ( #864892=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I am trying to increment the following number in Perl but it wont do it: AA825944001 --> AA825944002

I have tried the following and it does not work, it returns 1

$TranNum= $fields[12] +1; $fields[12] =~s/$fields[12]/$TranNum/g;

I assume it has to do with the alpha characters, but I have thousands of numbers in a column that I need to increment by 1. Any help will be appreciated.

Replies are listed 'Best First'.
Re: incrementing a number
by toolic (Bishop) on Oct 12, 2010 at 16:23 UTC
    use strict; use warnings; my $s = 'AA825944001'; $s++; print "$s\n"; __END__ AA825944002

    Please reformat your post -- remove all pre tags.

Re: incrementing a number
by chromatic (Archbishop) on Oct 12, 2010 at 16:23 UTC

    If your columns always match the regular expression [A-Z]+\d+, you can match and increment only the numeric portion:

    $fields[12] =~ s/(\d+)/ $1 + 1 /e;
Re: incrementing a number
by choroba (Cardinal) on Oct 12, 2010 at 16:24 UTC
    AA825944001 is not a number, it is a string. If you mean just the numeric part, you have to separate it, increment and glue together again.
    In fact, Perl can increment strings as well, just try
    $x='AA825944001'; print ++$x;

    Just be careful with strings like AA99 - is AB00 what you really want to get from them?

Re: incrementing a number
by Khen1950fx (Canon) on Oct 12, 2010 at 19:09 UTC
    To clear up some confusion about ++: To increment by one, use, as toolic did, s++. What about ++s? That's a little different. I put a simple sample together that'll help you get a feeling for it.
    #!/usr/bin/perl use strict; use warnings; my(@fields) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); print $fields[12]++, "\n", ++$fields[12], "\n";
    You could also use a counter that will automatically increment each field everytime it's run:
    #!/usr/bin/perl use strict; use warnings; use Tie::Counter; tie my $counter => 'Tie::Counter'; my @fields = qw/1 2 3 4 5 6 7 8 9 10 11 12 13/; foreach my $field(@fields) { print " $counter $field\n"; }
Re: incrementing a number
by roboticus (Chancellor) on Oct 12, 2010 at 18:33 UTC


    You've probably received a fine answer already, as I see some answers here from reliable names. But your text is so small, that I can't really make out your post with my ancient eyes. Please don't post in a tiny font.

    Thanks in advance...


      Not that the OP should be using <pre> tags, but I fix that vision problem on my end with some CSS:

      /* Adjust pre/code font, size */ pre { font-size: 20px; font-family: DejaVu Sans Mono; color: MediumAqu +aMarine } pre.code.c, tt.codetext, tt.inlinecode { font-size: 20px; font-family: + consolas; color: LightSteelBlue } .code { overflow: auto; }




        Thanks, I think I'll start playing with that!

        Someday ... I'll have to get with it and do some web programming....


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://864892]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-02-01 16:43 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (11 votes). Check out past polls.