[31934] in Perl-Users-Digest
Perl-Users Digest, Issue: 3197 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 4 00:09:23 2010
Date: Wed, 3 Nov 2010 21:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 3 Nov 2010 Volume: 11 Number: 3197
Today's topics:
Re: Could you give me some explanation on this Perl lin <m@rtij.nl.invlalid>
Re: Could you give me some explanation on this Perl lin <sherm.pendley@gmail.com>
Re: Could you give me some explanation on this Perl lin <rxjwg98@gmail.com>
Re: Foreach <kst-u@mib.org>
Re: Foreach <tzz@lifelogs.com>
Re: Foreach <tadmc@seesig.invalid>
Re: Foreach <jimsgibson@gmail.com>
Re: Foreach <kst-u@mib.org>
Re: Foreach <uri@StemSystems.com>
printf and print <bandar.coal@gmail.com>
Re: printf and print <tadmc@seesig.invalid>
Re: printf and print <bandar.coal@gmail.com>
Re: printf and print <tadmc@seesig.invalid>
Re: printf and print <m@rtij.nl.invlalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 3 Nov 2010 20:58:17 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Could you give me some explanation on this Perl line
Message-Id: <pfr8q7-6ii.ln1@news.rtij.nl>
On Wed, 03 Nov 2010 13:22:24 +0100, Wolf Behrenhoff wrote:
> Also always "use strict; use warnings;" in all your programs. The above
> example will not even compile with "use strict". That is a clear sign
> that something should be changed in the code.
$ cat t.pl
#!/usr/bin/perl
use warnings;
use strict;
eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
$ perl t.pl
Although I agree, you should not set variables like this, the code does
compile under strict.
M4
------------------------------
Date: Wed, 03 Nov 2010 16:15:44 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Could you give me some explanation on this Perl line
Message-Id: <m2k4kugo3z.fsf@sherm.shermpendley.com>
fl <rxjwg98@gmail.com> writes:
> I just begin Perl learning. A sample Perl program is relevant to my
> project. After reading some on Perl, I still do not understand the
> first line of this sample, see below. Could you help me on its
> meaning? Thanks in advance.
>
> eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
If you're just beginning, the major lesson to learn from the above is:
DO NOT write such a mess!
Seriously! Deliberately terse one-liners have a place, in obfuscated
code contests, Perl Golf, and clever .sig files. But, if you intend to
write and maintain useful code that gets Real Work (tm) done, clarity
is far more important than brevity.
What's more, the use of eval in the above to construct a variable name
from a value is pure evil to be avoided whenever possible - use a real
hash instead of abusing the symbol table hash.
A simple, clear way to write the above would be:
#!/usr/bin/perl
use warnings;
use strict;
my %args;
foreach my $arg (@ARGV) {
if ($arg =~ /^(\w+)=(.*)/) {
$args{$1} = $2;
}
}
For anything other than a learning exercise though, I wouldn't bother
reinventing that wheel - GetOpt::Std and GetOpt::Long are sufficiently
round, and learning to avoid unnecessary work by taking advantage of
existing modules is a *big* part of learning Perl.
sherm--
--
Sherm Pendley
<http://camelbones.sourceforge.net>
Cocoa Developer
------------------------------
Date: Wed, 3 Nov 2010 16:38:27 -0700 (PDT)
From: fl <rxjwg98@gmail.com>
Subject: Re: Could you give me some explanation on this Perl line
Message-Id: <c951185a-66bd-4589-837f-b8742b63fe3e@k22g2000yqh.googlegroups.com>
On 3 nov, 16:15, Sherm Pendley <sherm.pend...@gmail.com> wrote:
> fl <rxjw...@gmail.com> writes:
> > I just begin Perl learning. A sample Perl program is relevant to my
> > project. After reading some on Perl, I still do not understand the
> > first line of this sample, see below. Could you help me on its
> > meaning? Thanks in advance.
>
> > eval "\$$1=3D\$2" while @ARGV && $ARGV[0]=3D~ /^(\w+)=3D(.*)/ && shift;
>
> If you're just beginning, the major lesson to learn from the above is:
>
> =A0 =A0 DO NOT write such a mess!
>
> Seriously! Deliberately terse one-liners have a place, in obfuscated
> code contests, Perl Golf, and clever .sig files. But, if you intend to
> write and maintain useful code that gets Real Work (tm) done, clarity
> is far more important than brevity.
>
> What's more, the use of eval in the above to construct a variable name
> from a value is pure evil to be avoided whenever possible - use a real
> hash instead of abusing the symbol table hash.
>
> A simple, clear way to write the above would be:
>
> =A0 =A0 #!/usr/bin/perl
>
> =A0 =A0 use warnings;
> =A0 =A0 use strict;
>
> =A0 =A0 my %args;
> =A0 =A0 foreach my $arg (@ARGV) {
> =A0 =A0 =A0 =A0 if ($arg =3D~ /^(\w+)=3D(.*)/) {
> =A0 =A0 =A0 =A0 =A0 =A0 $args{$1} =3D $2;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
>
> For anything other than a learning exercise though, I wouldn't bother
> reinventing that wheel - GetOpt::Std and GetOpt::Long are sufficiently
> round, and learning to avoid unnecessary work by taking advantage of
> existing modules is a *big* part of learning Perl.
>
> sherm--
>
> --
> Sherm Pendley
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<h=
ttp://camelbones.sourceforge.net>
> Cocoa Developer
Thanks to all of you. From what you said, I know this line's function
and where I can get the appropriate way (GetOpt) to do that. The
feedbacks make me avoid some bad thing on this learning.
------------------------------
Date: Wed, 03 Nov 2010 10:34:33 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Foreach
Message-Id: <lnmxpqe2fq.fsf@nuthaus.mib.org>
Sherm Pendley <sherm.pendley@gmail.com> writes:
> perler wannabe <bandar.coal@gmail.com> writes:
>
>> I read from a book that "for" is actually "foreach".
>
> They're equivalent, yes.
>
>> And foreach is preferable in Perl.
>
> I think it's more accurate to say that Perl-style looping over lists
> is preferable over C-style loops with three arguments. Iterating over
> the items in a list is a very common operation in Perl, and Perl-style
> loops provide a more elegant way of doing that.
Agreed, in most cases. But sometimes it's useful (or even necessary) to
know inside the loop where you are in the loop, for example if you want
to treat the first last item specially.
For example, if I want to print each command-line argument on a separate
line, I might write (assume the usual "use strict; use warnings;" and so
forth):
foreach my $arg (@ARGV) {
print "$arg\n";
}
But if I want to print all the arguments on one line, as /bin/echo
does, a simple foreach doesn't work; I have to know when I'm
processing the last argument:
foreach my $i (0 .. $#ARGV) {
print "$ARGV[$i]";
if ($i < $#ARGV) {
print " ";
}
else {
print "\n";
}
}
(Or I might use a C-style for loop instead of the foreach.)
In short, one limitation of using foreach to iterate over a list is
that, inside the loop, the only information you have is the value
of the current element. Sometimes that's all you need, but in
the cases where you need more, you have to re-structure the loop.
(Though I could have added "my $arg = $ARGV[$i];" if I had a lot
of references to the current argument.)
Is there a cleaner idiom for this kind of thing that I'm not
thinking of? I suspect the answer is no.
(Yes, I could have used join() here; this is a simplified example
of a more general issue.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Wed, 03 Nov 2010 12:43:45 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Foreach
Message-Id: <87wrou481a.fsf@lifelogs.com>
On Wed, 03 Nov 2010 10:34:33 -0700 Keith Thompson <kst-u@mib.org> wrote:
KT> Agreed, in most cases. But sometimes it's useful (or even necessary) to
KT> know inside the loop where you are in the loop, for example if you want
KT> to treat the first last item specially.
As a general problem this is well-known; many algorithms and constructs
are stateless (they work at any offset during the iteration) while
others aren't. I'm not sure of the technical term :)
KT> Is there a cleaner idiom for this kind of thing that I'm not
KT> thinking of? I suspect the answer is no.
I usually think about the reasons why I need the first and last thing.
Sometimes I do this:
# same for the last element with pop(), obviously
process_first(shift @data);
process_rest($_) foreach @data;
Sometimes I keep the offset manually:
my $i = 0;
foreach my $x (@data)
{
...
}
Sometimes I do a Lisp-style "shift while true" loop:
# @data is a copy of the original data
while (@data)
{
my $element = shift @data;
do_first_thing() unless scalar @data;
do_general_thing();
}
Sometimes I use my own Every.pm module to do periodic actions:
foreach my $x (@data)
{
...
do_periodic_thing() if every(2);
}
...but mostly, I wish for the Common Lisp `loop' statement :) See
http://www.gnu.org/software/emacs/manual/html_mono/cl.html#Loop-Facility
for details on how that works.
Ted
------------------------------
Date: Wed, 03 Nov 2010 15:11:33 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Foreach
Message-Id: <slrnid3gs0.po8.tadmc@tadbox.sbcglobal.net>
Keith Thompson <kst-u@mib.org> wrote:
> Sherm Pendley <sherm.pendley@gmail.com> writes:
>> perler wannabe <bandar.coal@gmail.com> writes:
>>
>>> I read from a book that "for" is actually "foreach".
>>
>> They're equivalent, yes.
>>
>>> And foreach is preferable in Perl.
>>
>> I think it's more accurate to say that Perl-style looping over lists
>> is preferable over C-style loops with three arguments. Iterating over
>> the items in a list is a very common operation in Perl, and Perl-style
>> loops provide a more elegant way of doing that.
>
> Agreed, in most cases.
From that, I thought you were going to present a case where a C-style
loop with three arguments was preferable over Perl-style looping over
lists...
> But sometimes it's useful (or even necessary) to
> know inside the loop where you are in the loop,
You can do that using a Perl-style loop.
> But if I want to print all the arguments on one line, as /bin/echo
> does, a simple foreach doesn't work;
That was an unfortunate choice of example, as that can be done with:
print "@ARGV\n";
:-)
> I have to know when I'm
> processing the last argument:
>
> foreach my $i (0 .. $#ARGV) {
> print "$ARGV[$i]";
perldoc -q vars
> (Or I might use a C-style for loop instead of the foreach.)
foreach (my $i=0; $i<=$#ARGV; $i++) {
or
foreach (my $i=0; $i<@ARGV; $i++) {
I'd say that a Perl style loop "foreach my $i (0 .. $#ARGV)" was
preferable here too.
Were you going to present a case where a C-style loop was preferable?
> In short, one limitation of using foreach to iterate over a list is
> that, inside the loop, the only information you have is the value
> of the current element. Sometimes that's all you need,
Like in all of the code that you posted.
Were you going to present a case where a C-style loop was preferable?
> Is there a cleaner idiom for this kind of thing that I'm not
> thinking of?
print "$ARGV[0] "; # do something with the first arg
foreach my $i (1..$#ARGV-1) {
print "$ARGV[$i] ";
}
print "$ARGV[-1]\n"; # do something with the last arg
You can generate a list of indexes using either style of loop, but
a Perl-style loop seems preferable for that.
You have presented a case where you need to know the index, but that
can be done with either type of loop.
So, what was it that Sherm said that you (intended to) disagree with?
--
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.
------------------------------
Date: Wed, 03 Nov 2010 15:20:35 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Foreach
Message-Id: <031120101520355972%jimsgibson@gmail.com>
In article <slrnid3gs0.po8.tadmc@tadbox.sbcglobal.net>, Tad McClellan
<tadmc@seesig.invalid> wrote:
> Keith Thompson <kst-u@mib.org> wrote:
> > Sherm Pendley <sherm.pendley@gmail.com> writes:
> >> perler wannabe <bandar.coal@gmail.com> writes:
> >>
> >>> I read from a book that "for" is actually "foreach".
> >>
> >> They're equivalent, yes.
> >>
> >>> And foreach is preferable in Perl.
> >>
> >> I think it's more accurate to say that Perl-style looping over lists
> >> is preferable over C-style loops with three arguments. Iterating over
> >> the items in a list is a very common operation in Perl, and Perl-style
> >> loops provide a more elegant way of doing that.
> >
> > Agreed, in most cases.
>
>
> From that, I thought you were going to present a case where a C-style
> loop with three arguments was preferable over Perl-style looping over
> lists...
One thing you can do with C-style for loops that you cannot do with
Perl-style loops is modify the value of the loop index inside the loop:
my $save;
for( my int $i = 0; $i < @ARGS; $i++ ) {
if( $ARGS[$i] =~ /-savenext/ ) {
$save = $ARGS[++$i];
}
}
--
Jim Gibson
------------------------------
Date: Wed, 03 Nov 2010 18:21:18 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Foreach
Message-Id: <lniq0deve9.fsf@nuthaus.mib.org>
Tad McClellan <tadmc@seesig.invalid> writes:
> Keith Thompson <kst-u@mib.org> wrote:
>> Sherm Pendley <sherm.pendley@gmail.com> writes:
>>> perler wannabe <bandar.coal@gmail.com> writes:
>>>
>>>> I read from a book that "for" is actually "foreach".
>>>
>>> They're equivalent, yes.
>>>
>>>> And foreach is preferable in Perl.
>>>
>>> I think it's more accurate to say that Perl-style looping over lists
>>> is preferable over C-style loops with three arguments. Iterating over
>>> the items in a list is a very common operation in Perl, and Perl-style
>>> loops provide a more elegant way of doing that.
>>
>> Agreed, in most cases.
>
>
> From that, I thought you were going to present a case where a C-style
> loop with three arguments was preferable over Perl-style looping over
> lists...
No, I suppose I wasn't entirely clear. The distinction I was making
was between iterating over the elements of a list and iterating
over the indices of a list.
[...]
>> I have to know when I'm
>> processing the last argument:
>>
>> foreach my $i (0 .. $#ARGV) {
>> print "$ARGV[$i]";
>
>
> perldoc -q vars
Right, lose the quotes. (That was an editing error.)
>> (Or I might use a C-style for loop instead of the foreach.)
>
>
> foreach (my $i=0; $i<=$#ARGV; $i++) {
> or
> foreach (my $i=0; $i<@ARGV; $i++) {
>
>
> I'd say that a Perl style loop "foreach my $i (0 .. $#ARGV)" was
> preferable here too.
Agreed.
> Were you going to present a case where a C-style loop was preferable?
No, I wasn't planning on it. 8-)}
>> In short, one limitation of using foreach to iterate over a list is
>> that, inside the loop, the only information you have is the value
>> of the current element. Sometimes that's all you need,
>
>
> Like in all of the code that you posted.
No, in the code I posted I need to know when I'm processing the last
item.
> Were you going to present a case where a C-style loop was preferable?
>
>> Is there a cleaner idiom for this kind of thing that I'm not
>> thinking of?
>
> print "$ARGV[0] "; # do something with the first arg
> foreach my $i (1..$#ARGV-1) {
> print "$ARGV[$i] ";
> }
> print "$ARGV[-1]\n"; # do something with the last arg
Hmm. Yes, that certainly works, but there's some duplication
of code. In this case the duplicated code is just a print, but as
I said this is a simple example.
> You can generate a list of indexes using either style of loop, but
> a Perl-style loop seems preferable for that.
>
> You have presented a case where you need to know the index, but that
> can be done with either type of loop.
[...]
More generally, here's the kind of thing I have in mind (pseudo-code):
First version:
foreach my $elem (@list) {
do_a_whole_bunch_of_stuff_with($elem); # multiple lines of code
}
In the second version, I need to add some minor functionality:
foreach my $elem (@list) {
do_a_whole_bunch_of_stuff_with($elem); # multiple lines of code
if (this is the first element) {
do_this_with($elem);
}
elsif (this is the last element) {
do_that_with($elem);
}
else {
do_the_other_thing_with($elem);
}
}
What I'm vaguely wishing for, but don't particularly expect to get,
is a clean way to detect, from inside the loop, whether I'm looking
at the first or last element *without* having to restructure the
loop to keep track of the current index.
I can't even think of a good way to design a language feature that
would support this (let alone think about how to implement it),
so I'll just live with the way it is now.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Wed, 03 Nov 2010 23:34:36 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Foreach
Message-Id: <877hgtkbhv.fsf@quad.sysarch.com>
>>>>> "KT" == Keith Thompson <kst-u@mib.org> writes:
KT> No, in the code I posted I need to know when I'm processing the last
KT> item.
so use a while/shift loop instead. i stay away from indexing arrays as
much as possible as there is usually a better way to deal with things.
while( defined( my $elem = shift @array ) ) {
last_elem( $elem) unless @array ;
rest of code
}
KT> foreach my $elem (@list) {
KT> do_a_whole_bunch_of_stuff_with($elem); # multiple lines of code
KT> if (this is the first element) {
KT> do_this_with($elem);
KT> }
KT> elsif (this is the last element) {
KT> do_that_with($elem);
KT> }
KT> else {
KT> do_the_other_thing_with($elem);
KT> }
KT> }
you can just add a flag to the while/shift loop above to handle the
first element. but if all your work code is in subs, then a 3 layer set
is fine too:
my $elem = shift @array ;
do_this_first($elem);
while( defined( my $elem = shift @array ) ) {
if ( @array ) {
do_middle_elem( $elem ) ;
next ;
}
do_last_elem( $elem ) ;
}
or split the array with splice/slice and work each part separately
(untested):
my( $first, @middle ) = @array ;
my $last = pop @middle ;
do_first( $first ) ;
foreach my $elem ( @middle ) {
do_middle( $elem ) ;
}
do_last( $last ) ;
not too fugly. and better than indexing. perl6 will be able to loop over
indexes and elems together like each does for hashes.
there is even a way to do it with the range flipflop op. it has a neat
return value where the last count value it returns is in float with an
E0 on it.
this is tested and works. odd looking but fun!
my @elems = qw( a b c d ) ;
while( my $ind = (@elems .. @elems == 1) ) {
my $elem = shift @elems ;
print "IND $ind\n" ;
if ( $ind == 1 ) {
print "FIRST $elem\n" ;
next ;
}
if ( $ind =~ /e/i ) {
print "LAST $elem\n" ;
next ;
}
print "MID $elem\n" ;
}
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 3 Nov 2010 08:58:28 -0700 (PDT)
From: perler wannabe <bandar.coal@gmail.com>
Subject: printf and print
Message-Id: <c52826a3-d4bb-4f29-a539-0ff345a1d82e@32g2000yqz.googlegroups.com>
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
------------------------------
Date: Wed, 03 Nov 2010 11:17:36 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: printf and print
Message-Id: <slrnid3358.ph5.tadmc@tadbox.sbcglobal.net>
perler wannabe <bandar.coal@gmail.com> wrote:
> Here is my script:
If you say so...
> #!/usr/bin/perl
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?
> chomp(my @string = <STDIN>);
>
> print "1234567890" x 6,"\n";
> foreach (@string) {
> printf "%20s\n";
^^^^
^^^^ what does that get replaced with?
> }
>
> This gives me the output I want
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?
> which is: my strings reight-justified,
> 20 characters column as the following:
>
> 1234567890123456789012345678901234567890123456789012345678901234567890
> perL
> c++
> javascript
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?
> 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?
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.
------------------------------
Date: Wed, 3 Nov 2010 10:10:51 -0700 (PDT)
From: perler wannabe <bandar.coal@gmail.com>
Subject: Re: printf and print
Message-Id: <e80d5a7e-4545-4365-b5a3-d1a02804f5f4@d8g2000yqf.googlegroups.com>
On Nov 3, 11:17=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
> perler wannabe <bandar.c...@gmail.com> wrote:
> > Here is my script:
>
> If you say so...
>
> > #!/usr/bin/perl
>
> You should ask for all the help you can get by enabling warnings
> and strictures in every Perl program:
>
> =A0 =A0 use warnings;
> =A0 =A0 use strict;
>
> Have you seen the Posting Guidelines that are posted here frequently?
>
> > chomp(my @string =3D <STDIN>);
>
> > print "1234567890" x 6,"\n";
> > foreach (@string) {
> > printf "%20s\n";
>
> =A0 =A0 =A0 =A0 =A0 ^^^^
> =A0 =A0 =A0 =A0 =A0 ^^^^ what does that get replaced with?
>
> > }
>
> > This gives me the output I want
>
> 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?
>
> > which is: my strings reight-justified,
> > 20 characters column as the following:
>
> > 1234567890123456789012345678901234567890123456789012345678901234567890
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0perL
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 c++
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 javascript
>
> 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?
>
> > 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
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 c++
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 javascript
>
> > what makes this distinct behavior?
>
> printf's format string does not contain any format specifiers, so all
> of the rest of the arguments to printf (ie. "\n") are ignored.
>
> =A0 =A0 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 =3D <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
------------------------------
Date: Wed, 03 Nov 2010 14:42:41 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: printf and print
Message-Id: <slrnid3f5r.po8.tadmc@tadbox.sbcglobal.net>
perler wannabe <bandar.coal@gmail.com> wrote:
> On Nov 3, 11:17 pm, Tad McClellan <ta...@seesig.invalid> wrote:
>> Have you seen the Posting Guidelines that are posted here frequently?
>> Have you seen the Posting Guidelines that are posted here frequently?
>> printf's format string does not contain any format specifiers, so all
>> of the rest of the arguments to printf (ie. "\n") are ignored.
>> --
>> 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.
It is bad manners to quote .sigs.
Have you seen the Posting Guidelines that are posted here frequently?
> Hi Tad, thanks for your reply.
Yeah, right.
> Indeed I have to rewrite my script and
> the output to post it here.
You are on your own then.
I don't have the time to help troubleshoot programs that do not even exist.
So long.
> print "1234567890" x 6,"\n"; #ruler
> And with printf, the first string
> doesn't
I have already answered your question.
Did you mean to ask a different question?
--
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.
------------------------------
Date: Wed, 3 Nov 2010 21:02:55 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: printf and print
Message-Id: <for8q7-6ii.ln1@news.rtij.nl>
On Wed, 03 Nov 2010 10:10:51 -0700, perler wannabe wrote:
> 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
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3197
***************************************