CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Perl Bug?????



Sep 28, 2000, 6:26 AM

Post #1 of 2 (984 views)
Perl Bug????? Can't Post

Here's a seemingly simple one....

The auto-decrement won't work.

$var = 'AB';
print ++$var;

# yields: AC

$var = 'AB';
print --$var;

# yields: -1

I've tried sprintf and everything else.

I have PERL 5.004 installed on a Sun box.


Enthusiast / Moderator

Sep 28, 2000, 6:56 AM

Post #2 of 2 (984 views)
Re: Perl Bug????? [In reply to] Can't Post

From 'perlop':

Auto-increment and Auto-decrement

"++" and "--" work as in C. That is, if placed before a
variable, they increment or decrement the variable before
returning the value, and if placed after, increment or
decrement the variable after returning the value.

The auto-increment operator has a little extra builtin magic
to it. If you increment a variable that is numeric, or that
has ever been used in a numeric context, you get a normal
increment. If, however, the variable has been used in only
string contexts since it was set, and has a value that is
not the empty string and matches the pattern /^[a-zA-Z]*[0-
9]*$/, the increment is done as a string, preserving each
character within its range, with carry:

print ++($foo = '99'); # prints '100'
print ++($foo = 'a0'); # prints 'a1'
print ++($foo = 'Az'); # prints 'Ba'
print ++($foo = 'zz'); # prints 'aaa'

The auto-decrement operator is not magical.

Auto-decrement on strings is undefined -- that is, the string is first converted to a number somehow -- because it is not a consistent process. What is --"a"?

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives