Print - format / presentation question

C

Chris Vidal

This works fine

print "*" x 10;
print "\n";

But is there a way to print *'s then a newline in one line

print "*" x 10"\n"; doesnt work.

Tks
 
J

James Willmore

This works fine
print "*" x 10;
print "\n";

But is there a way to print *'s then a newline in one line

print "*" x 10"\n"; doesnt work.

Try:

print "*" x 10, "\n";

HTH

Jim
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mothra said:
[snipped]
print "*" x 10"\n"; doesnt work.
print "*" x10 . "\n";

Careful:

$ perl -le 'print "*"x10."\n"'
String found where operator expected at -e line 1, near "10."\n""
(Missing operator before "\n"?)
syntax error at -e line 1, near "10."\n""
Execution of -e aborted due to compilation errors.


- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPzQuYWPeouIeTNHoEQIDSQCgx6lt/s8F+KNxNQvFs6yQIATr/JMAoMig
62FX+XjzMl2FPD1fhmLGL1+Q
=H16C
-----END PGP SIGNATURE-----
 
M

Matt Garrish

Eric J. Roode said:
Careful:

$ perl -le 'print "*"x10."\n"'
String found where operator expected at -e line 1, near "10."\n""
(Missing operator before "\n"?)
syntax error at -e line 1, near "10."\n""
Execution of -e aborted due to compilation errors.

Are you just trying to show that you can screw up the syntax, or are you
forgetting about associativity?

perl -le 'print("*"x10)."\n"'

I'll assume you were trying to make the former point, but your observation
doesn't make his response wrong, and he never claimed that it would work as
you wrote it. Whichever the case, though, you should have explained better
than to say "careful" and shown the error.

Matt
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Are you just trying to show that you can screw up the syntax, or are
you forgetting about associativity?

perl -le 'print("*"x10)."\n"'

Are you just trying to show that you can misuse parentheses, or have you
forgotten the "if it looks like a function call, it is a function call"
rule?

I'll assume you were trying to make the former point, but your
observation doesn't make his response wrong, and he never claimed that
it would work as you wrote it.

The way I wrote it was an exact copy, modulo whitespace, of what Mothra
posted. I happen to have respect for Mothra, based on his or her posts
here, which is why I didn't post a flame, and why I felt that nothing
more than "careful" plus an error dump was needed for Mothra to
understand that s/he should have known better than to post a quick
solution without making sure that it was correct.

However, I made the mistake of thinking that whitespace didn't matter to
Mothra's solution. It does. Not because of precedence, not because of
associativity, but because "10." is interpreted as a number, rather than
a number plus the concatenation operator. My mistake, not Mothra's; mea
culpa. But I didn't think that my mistake warranted a flame.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPzRB8WPeouIeTNHoEQKduQCg+THPa8sTZ0puBtlJ0J6WsHJ8imsAoIuf
3KWqgyDx/pe2UpejwcivLX4l
=AGte
-----END PGP SIGNATURE-----
 
M

Matt Garrish

Eric J. Roode said:
Are you just trying to show that you can misuse parentheses, or have you
forgotten the "if it looks like a function call, it is a function call"
rule?

The point was to show to you why the code you posted didn't work (i.e.,
10."\n" is misinterpreted if you leave out the whitespace). The parentheses
are there to return the . to the operator that it was in the original
example, and still make it work without whitespace (which I thought it was
your intention to show wouldn't)..
The way I wrote it was an exact copy, modulo whitespace, of what Mothra
posted.

Which means it was *not* an exact copy.
I happen to have respect for Mothra, based on his or her posts
here, which is why I didn't post a flame, and why I felt that nothing
more than "careful" plus an error dump was needed for Mothra to
understand that s/he should have known better than to post a quick
solution without making sure that it was correct.

Regardless, don't you think it might have been helpful to the OP to explain
why you were posting?
However, I made the mistake of thinking that whitespace didn't matter to
Mothra's solution. It does. Not because of precedence, not because of
associativity, but because "10." is interpreted as a number, rather than
a number plus the concatenation operator.

Which breaks the proper associativity of the concatenation operator in the
example posted by Mothra. If you want to look at it as going from an
operator to not an operator that's fine with me, but it's not really here or
there.
But I didn't think that my mistake warranted a flame.

Actually, there was no intent to flame, I just couldn't figure out why you
would post that error when it was of your own creation (I assumed you knew
why it wasn't working, but hadn't bothered to elaborate). Reread the first
line as a bewildered question rather than as the attack you seem to think it
is. As I've said, it made no sense why you were posting that error other
than to try and show how you could break his code, which isn't the most
challenging thing to do in Perl.

Matt
 
M

Matt Garrish

Sam Holden said:
Since it won't work with those parentheses that was a pretty silly way
of going about it.

C:\>perl -le "print('*'x10).'\n'"
**********

C:\>

What doesn't work?

Matt
 
M

Matt Garrish

Just to clarify, I thought (mistakenly so) that I didn't need the extra
parentheses from the following:

print(("*"x10)."\n");

since when I ran it from the command line it looked like it produced the
same result.

Matt
 
M

Matt Garrish

Sam Holden said:
(Keeping the '\n' strangeness you
introduced - I assume to get around DOS' command line parsing.)

I was just in a rush. It's actually what tipped me off to what was giving
the extra line when I spotted that.

Matt
 
T

Tad McClellan

Matt Garrish said:
C:\>perl -le "print('*'x10).'\n'"
**********

C:\>

What doesn't work?


perl -le "print('*'x10)"

works exactly the same.

That's a clue, the '\n' part is in void context, it doesn't get output.

Enabling warnings often provides clues.
 
M

Matt Garrish

Tad McClellan said:
Enabling warnings often provides clues.

Which I normally do (see previous post for the working example I started
with). I didn't want to be guilty of tinkering with the example provided by
Eric (which was his downfall earlier), and since he didn't have warnings
enabled, and since I already knew the original was parsable... I should have
known I was making too many assumptions.

In writing it for the command line, I decided to tinker a little and see if
perl would still handle the expression without the outer braces, which it
did, albeit only in appearance, so I posted the one-liner without any
further thought. Not specifying the useless -l option would have been as
effective, as the lack of a newline would have been just as informative as
anything the warnings had to offer, but you are right.

Matt
 
M

Mothra

Hi Eric,

[snipped]
The way I wrote it was an exact copy, modulo whitespace, of what Mothra
posted. I happen to have respect for Mothra, based on his or her posts
here, which is why I didn't post a flame, and why I felt that nothing
more than "careful" plus an error dump was needed for Mothra to
understand that s/he should have known better than to post a quick
solution without making sure that it was correct.
I do my best to test before I post. (I like all who have been here for some
time
have made the mistake of posting bad code and have paid the price
for it) In this case I did test. Here is what I did.

use strict;
use warnings;
use diagnostics;

print "*" x10 . "\n";

I did not post the first 3 lines (stupid me) If I had it would
have been clearer for all.
However, I made the mistake of thinking that whitespace didn't matter to
Mothra's solution. It does.

The whitespace does make a difference! (I did the same thing ie:
left out the white space and received the same error that you did)
That is why I added the whitespaces.
Not because of precedence, not because of
associativity, but because "10." is interpreted as a number, rather than
a number plus the concatenation operator. My mistake, not Mothra's; mea

No problem :)

Mothra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top