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

When I auto-increment my variable only reaches 12 before cycling to 1. I'm using v5.8.7 on linux. Paring my code down to just what matters for this purpose the following code produces "1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9":
my $onlyUse = 0; while (my @unitRow = $csr2->fetchrow) { my $unit = $unitRow[1]; unless($seen{$unit}) { $seen{$unit}++; $onlyUse++; print " $onlyUse ";next; } }
As might be expected, I need to reach numbers higher than 12 in order for my code to work properly. My variable is both local AND named such that I can be certain no other variable exists by that name AND I pared its use down to merely increment, print yet it does this to me... Thanks in advance for any assistance.

Replies are listed 'Best First'.
Re: Auto-increment wraps at 12
by GrandFather (Saint) on Jan 16, 2008 at 23:55 UTC

    That code doesn't run stand alone so I've added a little support code. The result performs as I'd expect:

    use warnings; use strict; my $onlyUse = 0; my @unitRow; my $csr2 = bless {count => 0}; my %seen; while (my @unitRow = $csr2->fetchrow) { my $unit = $unitRow[1]; unless ($seen{$unit}) { $seen{$unit}++; $onlyUse++; print " $onlyUse "; next; } } sub fetchrow { my $self = shift; return () if $self->{count} > 20; return (0, ++$self->{count}); }

    Prints:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21

    Maybe you should show us some code that can be run and that reproduces your problem?

    It seems likely that the while loop is run multiple times when in the larger context of your code.


    Perl is environmentally friendly - it saves trees
Re: Auto-increment wraps at 12 (other half)
by tye (Sage) on Jan 17, 2008 at 00:17 UTC

    Add one line to your code so that it starts out:

    my $onlyUse = 0; print "\nStarting over!\n";

    and see if that doesn't shed some light on what is happening.

    You are looking at the place where $onlyUse is incremented but aren't paying attention to the place where $onlyUse is set to 0.

    - tye        

A reply falls below the community's threshold of quality. You may see it by logging in.