http://www.perlmonks.org?node_id=38529

spaz has asked for the wisdom of the Perl Monks concerning the following question: (strings)

The following code produces some bizzare output. Apparently, "a"++ = "b", and "z"++ = "aa". But if I ever try to convert $string to a number "$number = (int $string)" I get 0. Anybody know why? --snip!--
#!/usr/bin/perl my $string = "a"; for( $i = 0; $i < 64; $i++ ) { $string++; print "string = '$string'\n"; }
--snip!--

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Why does $string++ work the way it does?
by extremely (Priest) on Oct 26, 2000 at 11:54 UTC
    Well, you've discovered that '++' is magic on strings of letters. The magic works on strings like "aaaa" and "aaa111" but on strings like "1111aaaa" you will get 1112. If the string begins with a number then perl will whack off the text and treat it as a number. All the other math ops will do the same for a string that begins with a number.

    BTW, perl's definition of a number is pretty complex. "-1e3garg" + "+4ee3" will net you 1004.

    $a="-1e3ble"; $b="+4.33eeq"; $c="aaa999"; $d="a9z9z9"; print "a = $a and b = $b\n"; print "c = $c and d = $d\n"; print "a + b = ". $a+$b . "\n"; print "c + d = ". $c+$d . "\n"; print "int(b) = ". int($b) . "\n"; print "++a = ". ++$a . "\n"; print "++b = ". ++$b . "\n"; print "++c = ". ++$c . "\n"; print "++d = ". ++$d . "\n";

    try out that code and see what I mean. Note what happens to $c when the numbers flip and notice that $d goes poof. =)

      "-1e3garg" + "+4ee3" #gives me -996, not 1004
Re: Why does $string++ work the way it does?
by Fastolfe (Vicar) on Oct 26, 2000 at 05:51 UTC
    ++ is magic in that it knows how to deal with both numbers and strings. With numbers, it behaves just like a numeric increment, but since Perl is smart, it assumes that if you are ++'ing a string, that you want to change the letters around a bit.

    The alternative would be to convert it to a number (0) and increment that (1), which doesn't make a lot of sense if you start off with a string, so why not make something useful out of it?

    Note that this is the way that ++ differs from + and +=. The latter two operations require another argument, which can't really be anything but a number, which would convert your string to numeric form first. So if you really do want to end up with a 1 and start from a string, just use +1 or +=1 to get it.

Re: Why does $string++ work the way it does?
by davido (Cardinal) on Sep 14, 2003 at 03:18 UTC
    The ++ operator can act upon both numbers and strings in a predictable fashion. With strings, 'A' increments to 'B', and 'Z' to 'AA'. ...and so on.

    The magic of the ++ operator with respect to strings also exherts itself by providing the .. operator (range operator) with similar magic.

    Try this at home...

    my $x = 1; my $b = 'A'; $x++; # $x now equals 2. $b++; # $b now equals 'B'. my @numbers = (1..10); # @numbers now contains 1, 2, 3, .. 10. my @letters = ('A'..'Z'); # @letters now contains A, B, C, .. Z. my @more = ('A'..'AZ'); # @more now contains A, B, .. Z, AA, AB, ..AZ.

    It seems that ++ magic, and .. magic, are related. Don't be fooled into expecting -- to be magic too though, it isn't. Neither can you use the .. operator alone to create descending lists.

    my $b = 'Z'; $b--; # You don't get Y. my @array = (10..1); # You don't get 10, 9..1. my @letters = ('Z'..'A'); # You don't get the letters reversed.

    Have fun with ++ and .. but take care not to expect magic from --. -- is for numbers.

Re: Why does $string++ work the way it does?
by spaz (Pilgrim) on Oct 26, 2000 at 04:34 UTC
    sorry, let me pre the code
    #!/usr/bin/perl
    
    my $string = "a";
    
    for( $i = 0; $i < 64; $i++ ) {
        $string++;
        print "string = '$string'\n";
    }
    

    Originally posted as a Categorized Answer.