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

Hi Monks,

Can someone tell me why decrement operator is not working as increment operator with Strings ?

my $test = "a"; print $test."\n"; $test++; print $test."\n"; $test--; print $test."\n";


The above code prints b after increment and prints -1 after decrement.

Replies are listed 'Best First'.
Re: Increment/Decrement Operator with Strings
by wfsp (Abbot) on Sep 27, 2005 at 05:56 UTC
    In perlop:

    The auto-decrement operator is not magical.

Re: Increment/Decrement Operator with Strings
by jbrugger (Parson) on Sep 27, 2005 at 08:07 UTC
    I'm not fond of this kind of magic...
    # this works #!/usr/bin/perl -w use strict; my $a = "a"; print ++$a ."\n"; print ++$a ."\n"; print ++$a ."\n"; # this also works #!/usr/bin/perl -w use strict; my $a = "a8"; print ++$a ."\n"; print ++$a ."\n"; print ++$a ."\n"; # but this is unpredictable #!/usr/bin/perl -w use strict; my $a = "a8y"; print ++$a ."\n"; print ++$a ."\n"; print ++$a ."\n";
    update Or better as posted by sh1tn: see the difference
    perl -e '$char = "a"; print $char++, $/ for 1..20' perl -e '$char = "a0"; print $char++, $/ for 1..20' perl -e '$char = "a0a"; print $char++, $/ for 1..20'
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Increment/Decrement Operator with Strings
by reasonablekeith (Deacon) on Sep 27, 2005 at 09:51 UTC
    because, according to to Programming Perl...

    The autodecrement operator, however, is not magical.

    I guess you'll have to roll your own. Sorry.

    UPDATE:

    wfsp, I skimmed past your answer thinking you'd just posted a link to the docs, and didn't even read the last line, thinking it was a signature.

    ---
    my name's not Keith, and I'm not reasonable.
Re: Increment/Decrement Operator with Strings
by sh1tn (Priest) on Sep 27, 2005 at 07:57 UTC
    The operator works just fine on chars:
    # perl -e '$char = "a"; print $char++, $/ for 1..26'
    update: from perlop: "... 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]*\z/", the increment is done as a string, preserving each character within its range ..."
    Nowhere is said the same about the auto-decrement operator, so why should someone expect similar behaviour?


      Of course. But that is not the question.
      The OP asks about the decrement operator, not the increment.


      holli, /regexed monk/
Re: Increment/Decrement Operator with Strings
by ambrus (Abbot) on Sep 28, 2005 at 22:07 UTC
Re: Increment/Decrement Operator with Strings
by QM (Parson) on Sep 27, 2005 at 21:27 UTC
    I recall that someone had some nifty hack or obfu to simulate this, but I can't find it right now.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of