On Mon, Jul 23, 2001 at 11:47:43PM +0000, Lalith Wasantha wrote:
> Why do i get two different values for $a++ and $a+1?
> 
>    use strict;
>    my $a = $b = "AA";
>    $a++;
>    $b = $b + 1;
> 
>    print "\$a = $a\n\$b = $b\n";
> 
> Output: $a = AB
>         $b = 1

Because $a++ is magical.  No, seriously.

$a++, when applied to a string, increments it in a specific way.  Namely,
"a"++ becomes "b", "b"++ becomes "c", etc.  When you get to "z", "z"++
becomes "aa", "aa"++ becomes "ab", and so forth.  The case stays consistent,
so, in your case, you started out with all uppercase, and so each increment
stays in that case.

However, this magical increment doesn't work when you simply add 1.  When
you start using mathematical operators Perl automatically converts the
string into a number, in this case it converted $b into 0, and should've
emitted a warning if you have warnings on.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to