Gossamer Forum
Home : General : Perl Programming :

Please don't cheat...

Quote Reply
Please don't cheat...
Anyone know what:

print 2 + 2 x 2;

...prints?
Quote Reply
Re: [Paul] Please don't cheat... In reply to
6?


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Paul] Please don't cheat... In reply to
Code:
2 + 2 x 2 = 2 + (2 x 2) = 2 + (2 . 2) = 2 + 22 = 24

How'd I do?

--Philip
Links 2.0 moderator

Last edited by:

sponge: Aug 1, 2002, 9:56 PM
Quote Reply
Re: [Ian] Please don't cheat... In reply to
Sorry Ian, sponge was right, it's 24 Smile

It was a bit of a trick question :)

Last edited by:

Paul: Aug 2, 2002, 2:45 AM
Quote Reply
Re: [Paul] Please don't cheat... In reply to
I knew there would be... but someone had to state the obvious , right?

I guess 'x' is not multiply.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Please don't cheat... In reply to
 
print 2 * 5;
print 2 x 5;

Watch what happens Cool
Quote Reply
Re: [Paul] Please don't cheat... In reply to
Its a copy function of sorts... "Print 2, 5 times" 22222 Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Please don't cheat... In reply to
exactly, it can work with full strings as well.

Code:
print "spam!" x 3;
Quote Reply
Re: [Aki] Please don't cheat... In reply to
hmmm, handy, I think. Maybe good for those lengthy nbsp; lines or for drawing text graphs perhaps!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Please don't cheat... In reply to
It's an odd one, I haven't ever used it in my code yet, but I'll bet it's handy if you want to make dividers for sections in an email or reports for dot matrix (shiver) printers.

I was hoping this would work, i'd find it quite useful, but due to efficiency concerns, i guess not :(

Code:
$i = 10;
sub pickle { $i++ }
print pickle() x 10;
Quote Reply
Re: [Aki] Please don't cheat... In reply to
Code:
Quoteprint pickle() x 10;
hmmmm. Thats too bad. It won't print the output of a function ten times?


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Please don't cheat... In reply to
Code:
my $i = 0;
my $ch1 = '>';
my $ch2 = '<';
my $tab = ' ';

print "Content-type: text/plain\n\n";

for (0..20) {
if ($_ < 11) {
print $tab x $_ . $ch1 . "\n";
next;
}
print ( $tab x (20 - $_) . $ch2 . "\n" );
}

Or for things like that Cool
Quote Reply
Re: [Paul] Please don't cheat... In reply to
Now I was just saying to my g/f, I bet Paul knows how to do this! LOL


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Aki] Please don't cheat... In reply to
Code:
my $pickle = sub { return ($_[0] x $_[1]) };

print header();
print $pickle->('>', $_) for (1..10);

Last edited by:

Paul: Aug 2, 2002, 12:06 PM