Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: A Perl aptitude test

by artist (Parson)
on May 02, 2003 at 15:58 UTC ( #255055=note: print w/replies, xml ) Need Help??

in reply to A Perl aptitude test

If you want, you can make it trickier.
Example: Question 1:
print (print (2 * 3) + 3);


Replies are listed 'Best First'.
Re: Re: A Perl aptitude test
by silent11 (Vicar) on May 02, 2003 at 17:50 UTC
    I get 64. Why?

      You actually get a "6" followed by a "4".

      print (print (2 * 3) + 3);
      The inner print(2 * 3) is evaluated first: it prints 6, and returns the value "1" because the print succeeded. 1 + 3 is 4, so the outer print prints 4. There were no delimiters between the two digits, so they look like one number instead of two sequential digits.

      This brings up the original trick question quite nicely: the parentheses on print (2 * 3) + 3 make print() look like a function call, they aren't simply an arithmetic grouping operator in this case.


Re: Re: A Perl aptitude test
by Jonathan (Curate) on May 02, 2003 at 16:06 UTC
    hmmmm, I think they'd get it right by accident :-). As it stands no one has yet got the original correct so I don't think trying to make it harder is quite what I'm looking for!
    Question 1 is (IMO) a waste of time. I'm embarassed about it.
    Cheers anyway.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255055]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-05-27 15:59 GMT
Find Nodes?
    Voting Booth?