printf and print

P

perler wannabe

Hi,

Here is my script:

#!/usr/bin/perl

chomp(my @string = <STDIN>);

print "1234567890" x 6,"\n";
foreach (@string) {
printf "%20s\n";
}

This gives me the output I want which is: my strings reight-justified,
20 characters column as the following:

1234567890123456789012345678901234567890123456789012345678901234567890
perL
c++
javascript

but if I use printf instead of print in my script as the following:
printf "1234567890" x 6,"\n";

it will give me something like the following:
1234567890123456789012345678901234567890123456789012345678901234567890
perL
c++
javascript

what makes this distinct behavior?

Thanks
 
P

perler wannabe

If you say so...


You should ask for all the help you can get by enabling warnings
and strictures in every Perl program:

    use warnings;
    use strict;

Have you seen the Posting Guidelines that are posted here frequently?



          ^^^^
          ^^^^ what does that get replaced with?



No it doesn't.

You should copy/paste code, not (attempt to) retype it.

Have you seen the Posting Guidelines that are posted here frequently?



That is not the output from the code you posted.

That is not right justified.

That does not line up at 20 characters.

Have we entered the Twilight Zone?




printf's format string does not contain any format specifiers, so all
of the rest of the arguments to printf (ie. "\n") are ignored.

    printf "1234567890" x 6 . "\n";

but using printf() without any format specifiers is an abomination,
so don't do that in Real Code.

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.

Hi Tad, thanks for your reply. Indeed I have to rewrite my script and
the output to post it here. It's quite complicated, but in short, I
script in a pc having no internet connection, for now. I'll try to
post better now on :).

%20s inside of the double quote of my printf should bed replaced by $_
of course.

So my script should look like the following:

#!/usr/bin/perl


chomp(my @string = <STDIN>);


print "1234567890" x 6,"\n"; #ruler
foreach (@string) {
printf "%20s\n", $_;

}

forget about the retyped outputs... The output of the above script
gives me 20 chars right-justified. And with printf, the first string
doesn't
 
M

Martijn Lievaart

forget about the retyped outputs... The output of the above script gives
me 20 chars right-justified. And with printf, the first string doesn't

It does, but the "\n" is ignored in the printf. Please carefully reread
the printf documentation.

M4
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top