Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Welcome to the Monastery
 
PerlMonks  

Re: post-increment/pre-increment blues

by snafu (Chaplain)
on Jun 19, 2002 at 16:13 UTC ( [id://175832]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to post-increment/pre-increment blues

Fun exercise. So, I will take the challenge.
Now, I'll take a stab at this without doing any cheating. Then I will check my work and place it below in an 'update'.

First, my guess...

my $i = 0; # init $i and set to 0 print ++$i + 1; # answer: 1 (0 + 1) post-operation -> $i == 1 n +ow print $i++ + 1; # answer: 3 (2 + 1) post-operation -> $i == 2 n +ow print $i + $i++; # answer: 5 (2 + 3) post-operation -> $i == 3 n +ow print ++$i + ++$i; # answer: 7 (3 + 4) post-operation -> $i == 7 n +ow print ++$i + $i++ + 1; # answer: 17 (7 + 9 + 1) post-operation -> $i = += 17 now
Update:

Looks like I was way off :(

perl -Mstrict -e ' my $i = 0; print ++$i + 1,"\n"; print $i++ + 1,"\n"; print $i + $i++,"\n"; print ++$i + ++$i,"\n"; print ++$i + $i++ + 1,"\n"; ' 2 2 5 10 14
It looks like I had my pre- and post- backwards. Dumb mistake, really. I seem to have been making a lot of those lately. However, I did enjoy this little exercise.

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://175832]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.