CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Will you fall into the trap?

 



Paul
Enthusiast

Feb 10, 2002, 4:54 AM

Post #1 of 8 (24780 views)
Will you fall into the trap? Can't Post

I'm sure some people will cheat by just executing the code but what does the following print and why:

print (1 + 2) + 4;

....and how about:

print (1 + 2) + (4 + 4 + (3 + 3));


(This post was edited by WiredON.net on Feb 10, 2002, 4:57 AM)


freddo
User

Feb 10, 2002, 7:09 AM

Post #2 of 8 (24772 views)
Re: [WiredON.net] Will you fall into the trap? [In reply to] Can't Post

Nice one Wired!!! Smile

And i must admit that i cheated, and i remembered something...

[spoiler]the "print" syntax is the following:
print FILEHANDLE LIST
print LIST
print

So it ends when the list "(1+2)" is in.
print (1+2) evaluates to 3, as shown, for example with:
print print (1+2) + 4
that yields 35!!! The second question is now quite obvious too..[/spoiler]Thanks a lot, i never thought about such a use... Cool
Freddo

updated changed the font color to the new white background.

PS: Jasmine, is there a way one could set a font color in reguard to the background color (like [background]text[/background])??
;---

(This post was edited by freddo on Feb 12, 2002, 4:49 AM)


Jasmine
Administrator

Feb 10, 2002, 3:04 PM

Post #3 of 8 (24760 views)
Re: [freddo] Will you fall into the trap? [In reply to] Can't Post

For your spoilers, you can use the mnemonic [spoiler] text [/spoiler] markup tags, which will make the text white, just like the background.

Jasmine


fashimpaur
User

Feb 12, 2002, 4:41 AM

Post #4 of 8 (24740 views)
Re: [RedRum] Will you fall into the trap? [In reply to] Can't Post

Red Rum,

I saw this post and was curious. Rather than cheat, I thought
I would take a guess as to what happens in this and then test
my guess later. If I didn't do this, then no one will see the point
of your exercise.

So, here goes:

print (1 + 2) + 4;


In this, the (1 + 2) portion of this is the real arguments to the
print function. Therefore I believe that '3' should be printed to
STDOUT. Since you did not assign a variable to the return result,
$_ should have the value of the returned value from print(), which
if it printed is '1', plus '4', the sum of which is 5. So, if you print $_
next, it would be '5'.

Next, looking at your second example:


print (1 + 2) + (4 + 4 + (3 + 3));

I believe that '3' should be printed to STDOUT again and a print of
$_ would print 15, the sum of the return value '1' + 14.


Now, I will go test this.

Good example of how not to program.

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


fashimpaur
User

Feb 12, 2002, 4:54 AM

Post #5 of 8 (24736 views)
Re: [RedRum] Will you fall into the trap? [In reply to] Can't Post

Well, RedRum, I must now shout and jump and pat myself heartily on the back. This
is because I was right without cheating. I did not even see that Frodo posted a spoiler
which was fixed to obscure the spoiler by Jasmine until after my post!

Your example is exactly the reason this web site needs to be here. There are always
some things that will occur in programming and you will be hitting your head on the
keyboard as you try to figure out why the result is not what you expected.

Great Puzzle! Keep them coming!

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 12, 2002, 5:15 AM

Post #6 of 8 (24732 views)
Re: [fashimpaur] Will you fall into the trap? [In reply to] Can't Post

Smile

If the intention was to print 7 then you'd obviously need:

print 1 + 2 + 4;

-OR-

print (1 + 2 + 4);

-OR-

print (1 + (2 + 4));


(This post was edited by RedRum on Feb 12, 2002, 5:17 AM)


fashimpaur
User

Feb 12, 2002, 9:01 AM

Post #7 of 8 (24727 views)
Re: [freddo] Will you fall into the trap? [In reply to] Can't Post

freddo,

Did you really understand why the code:


print print(1+2) + 4;

prints 35?

If not, here is the skinny. If you did, then I post this for those that don't understand.
[spoiler]
Let me list the order of operations:

1) print (1 + 2)
2) store the returned value of step 1 in $_
3) print $_ + 4

So, the program prints a 3 to STDOUT.
The returned value of print is 1 if true and gets put in $_.
Then, the program prints the value of $_ + 4 which is 5 to STDOUT.

Because the 3 had no newline after it, the 5 was printed right beside it.
Thus, the result to STDOUT is:

35
[/spoiler]

Just trying to clarify your response.

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


hydpm
User

Jul 12, 2007, 9:24 AM

Post #8 of 8 (22440 views)
Re: [Paul] Will you fall into the trap? [In reply to] Can't Post

The out put is as follows:

[root@kingfisher Desktop]# cat test.pl
print (1 + 2) + 4;
print "\n";
print (1 + 2) + (4 + 4 + (3 + 3));
print "\n";

[root@kingfisher Desktop]# perl test.pl
3
3
[root@kingfisher Desktop]#

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives