Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

string operations of arithmetic

by xiaoyafeng (Deacon)
on Jun 24, 2011 at 12:22 UTC ( [id://911241]=perlquestion: print w/replies, xml ) Need Help??

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

here is code:
perl -e "$a = qq(ab); $a + 3; print $a;" # ab perl -e "$a = qq(ab1); $a ++; print $a;" # ab2 perl -e "$a = qq(ab1); for(1..10) {$a ++;} print $a;" #ac1
looks like perl5 string doesn't support operations of arithmetic but self-increment/decrement. My question is how override operator so as to fit on string? and take a look third statement, IMHO, ab11 is more reasonable. Any help?




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: string operations of arithmetic
by choroba (Cardinal) on Jun 24, 2011 at 12:37 UTC
    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, with carry:
    print ++($foo = '99'); # prints '100' print ++($foo = 'a0'); # prints 'a1' print ++($foo = 'Az'); # prints 'Ba' print ++($foo = 'zz'); # prints 'aaa'
Re: string operations of arithmetic
by MidLifeXis (Monsignor) on Jun 24, 2011 at 12:48 UTC
    perl -e "$a = qq(ab); $a + 3; print $a;"  # ab

    No. You didn't assign the results of $a + 3 back to $a, so I don't know what you would have expected it to do. The results I would expect would be 3

    IMHO, ab11 is more reasonable.

    This is documented in perlop (search for "Auto-increment"). The critical portion is:

    the increment is done as a string, preserving each character within its range, with carry [emphasis added]
    If it were implemented the way you propose, then the with carry part would no longer be valid. Since you are able to just do a string append of a numeric value to implement your use case ($base . $counter++), and it becomes much more difficult to do the string carry use case, IMHO it makes much more since to implement the string increment magic* as it currently stands.

    * I am not arguing for, or against, the string magic increment here, as I have not given enough thought to make an argument considering both sides. However, I can point to cases where I have used it in the past.

    --MidLifeXis

Re: string operations of arithmetic
by Anonymous Monk on Jun 24, 2011 at 12:26 UTC
    read perlnumber, cause strings are strings until you put them in numeric context
Re: string operations of arithmetic
by wind (Priest) on Jun 24, 2011 at 16:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://911241]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found