-4

I just want to know the reason if i use the (.) instead i got the result but + is doing arhtematic addition but how is that ASCII addition

 my $string = "ZZ";
 my $appendstring = $string+1;
 print $appendstring;

output

1

Expeccting

ZZ1
2
  • 4
    I don't believe that that output corresponds to that code.
    – mob
    Dec 15, 2015 at 22:53
  • @mob sorry i did not check that
    – Ankanna
    Dec 15, 2015 at 23:27

2 Answers 2

3

First of all, your question is very unclear, and your "example" (if you want to call it that) does not match reality, but in an effort to help whoever stumbles across this question in the future, I'm going to venture an answer anyway.

Let's clear up your example first:

$ perl -lwe '$x = "ZZ"; print $x + 1;'
Argument "ZZ" isn't numeric in addition (+) at -e line 1.
1

What I think you might have meant was:

$ perl -lwe '$x = "ZZ"; print ++$x;'
AAA

And the reason for that is explained in 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, with carry.


Edit: your updated question isn't any clearer than your original question, but now I think you're asking about string concatenation, which means you want the string concatenation operator: .

$ perl -lwe '$x = "ZZ"; print $x . 1;'
ZZ1

There is, however, a special case where you can use a string with the numeric addition operator and not generate a warning:

$ perl -lwe '$x = "0 but true"; print $x + 1;'
1

You also mentioned "ASCII addition", but I have no idea what that is or what you mean by that.

1
  • Thanks a lot for the answer i have been for @Matt Jacob
    – Ankanna
    Dec 15, 2015 at 23:26
2

According to this

this is the way to concatenate

use strict;
use warnings;

my $x = "4T";
my $y = 3;
print $x . $y;    # 4T3

but if you do this:

print $x + $y;  # 7
                # Argument "4T" isn't numeric in addition (+) at ...

Whenever you use the "+" perl tries to convert both values to numeric, if you provide a string and a number or 2 strings it'll take these as 0 and sum them. http://ideone.com/0LyEij

3
  • ya thanks for the answer.I tried using (.) concatenation but i want to know what is happening in the behind
    – Ankanna
    Dec 15, 2015 at 22:53
  • 1
    If you read the link i provided it states that whenever you use a "+" sign it tries to do a numerical addition trying to convert both values to numeric.
    – ivan
    Dec 15, 2015 at 22:57
  • 1
    also the output is 1 not ZZa according to the perl fiddle.
    – ivan
    Dec 15, 2015 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.