mistake in my for

O

Olaf \El BLanco\

for ($a=0; $a<$max; print "\t$a", $a++) {}
// OK, print 1 2 3 4 5 ... max

But

for ($a=0; $a<$max; $a++, print "\t$a") {}

// print 00 11 22 33 44... max-1max-1 ?
 
G

Glenn Jackman

At 2006-01-20 03:30PM said:
for ($a=0; $a<$max; print "\t$a", $a++) {}
// OK, print 1 2 3 4 5 ... max

But

for ($a=0; $a<$max; $a++, print "\t$a") {}

// print 00 11 22 33 44... max-1max-1 ?

You've got that backwards.

Beware of the comma operator versus calling print with 2 arguments.
 
B

Brian McCauley

Olaf said:
for ($a=0; $a<$max; print "\t$a", $a++) {}
// OK, print 1 2 3 4 5 ... max

But

for ($a=0; $a<$max; $a++, print "\t$a") {}

// print 00 11 22 33 44... max-1max-1 ?

You have the code and the output swapped.

Other than that, did you have a question?
 
O

Olaf \El BLanco\

Yes, I want to use the first for, but that start to print in number 0, and I
could't do it if $a = 0.
 
P

Paul Lalli

Please do not top-post. Post your reply *below* the text to which you
are replying, after trimming it appropriately.

<quoting fixed below>
Yes, I want to use the first for,

Why? What on earth makes you think you want to have executable code in
the header of a for loop? What possible reason do you have to do that
instead of:
for (my $a = 0; $a < max; $a++ ){
print "\t$a";
}
but that start to print in number 0, and I could't do it if $a = 0.

I have no idea what this means.

Show your *actual* code, and explain how whatever your attempt is is
not working for you. Show us your output, and then show us your
desired output to contrast.

Please note that in Perl, it's far more conventional to write:
for my $a (0..$max-1) {
print "\t$a";
}

than to use C-style for-loops.

Paul Lalli
 
J

Jim Cook

Please note that in Perl, it's far more conventional to write:
for my $a (0..$max-1)
than to use C-style for-loops.

Paul:

Does that have a potential memory hit if $max is exceedingly large, or
does perl handle it gracefully? If it handles it gracefully in this
case, is it true for all .. constructs, or does it actually generate the
list sometimes?
 
P

Paul Lalli

Does that have a potential memory hit if $max is exceedingly large, or
does perl handle it gracefully?

In "recent" versions of Perl, the list will be evaluated "lazilly",
meaning that the list is not actually built up to store large amount of
data in memory. I do not know a precise definition of "lazy" in this
case.
If it handles it gracefully in this
case, is it true for all .. constructs, or does it actually generate the
list sometimes?

I do not *believe* Perl will generate a list for anything which it can
evaluate lazily, but I do not know that for a fact. Someone more
familiar with the internals want to take a shot at this answer?

Paul Lalli
 
X

xhoster

Paul Lalli said:
In "recent" versions of Perl, the list will be evaluated "lazilly",
meaning that the list is not actually built up to store large amount of
data in memory. I do not know a precise definition of "lazy" in this
case.

As far as I can tell, a very simple .. in a for loop is the only case where
it is lazy.
I do not *believe* Perl will generate a list for anything which it can
evaluate lazily, but I do not know that for a fact.

It depends on what you mean by "can". perl can't do things in ways other
than what it was implemented to do, so in that sense it is a practicially a
tautology that perl does lazy evaluation everywhere it can. But if by
"can" you mean "it is conceptually feasible that it could", then it isn't
lazy wherever it can be. For example, it is conceptually feasible to be
lazy in the list part of a map or a grep, or in a reverse or array slice
used in the list of foreach or map or grep, but it is not lazy in any of
these contexts.

In each case, final memory usage in KB is printed:

perl -le 'foreach (1..10_000_000) { last}; \
print +(`ps -p $$ -o rss `)[1];'
1308

perl -le 'foreach (1..10_000_000,"extra") {last}; \
print +(`ps -p $$ -o rss `)[1];'
708460

perl -le 'foreach ("extra",(1..10_000_000)) {last};\
print +(`ps -p $$ -o rss `)[1];'
708460


perl -le 'foreach (@x[1..10_000_000]) { last}; \
print +(`ps -p $$ -o rss`)[1];'
905196

perl -le 'foreach (reverse 1..10_000_000) { last};\
print +(`ps -p $$ -o rss `)[1];'
708476

perl -le 'grep /foo/, 1..10_000_000;print +(`ps -p $$ -o rss `)[1];'
1337220

perl -le 'map "", 1..10_000_000;print +(`ps -p $$ -o rss `)[1];'
917644

This is perl, v5.8.7 built for x86_64-linux

Xho
 
O

Olaf \El Blanco\

Maybe this is not the correct group...
I am learning, I started with Perl 2 weeks ago... I was proving how work a
'for' with more than one sentence 'for(x;x;x,y)' that make 0,1,2,3,4,5,6,max
So, I wrote: for ($a=0; $a<$max; print "\t$a", $a++,) {}
But it didn't work, so, my question: What happend with the print when it
finish in a coma. And where I can find quickly answers in Perl sintaxis, I
use Suse Linux full documentation installed.
 
T

Tad McClellan

Glenn Jackman said:
You've got that backwards.

Beware of the comma operator versus calling print with 2 arguments.


Right.

So just to be clear:

In the 1st one above the comma is a "list separator", so print()
has 2 arguments to print.

In the 2nd one the comma is a "comma operator", it evaluates its
left operand and discards the result, then evaluates the right
operand, so print() has 1 argument to print.
 
P

Paul Lalli

I have asked you once already to post your reply *below* the text to
which you are replying. If you continue to be rude by not following
this convention, people will be far less likely to assist you.

<quoting fixed below. Again.>
Maybe this is not the correct group...

This is the correct group. You just have to follow some conventions
and make an attempt at explaining yourself.

I am learning, I started with Perl 2 weeks ago... I was proving how work a
'for' with more than one sentence 'for(x;x;x,y)' that make 0,1,2,3,4,5,6,max

Yes, I understand that's what you're doing. I still don't understand
*why* you want to do that. The standard way to write a for loop is
for (INIT; COND; INCR) {
STATEMENTS;
}

For some reason, you keep trying to put the statement along with the
incrementation. WHY?

Why are you insistant on having a statement within the incrementation,
and have the block itself be completely empty?
So, I wrote: for ($a=0; $a<$max; print "\t$a", $a++,) {}
But it didn't work, so, my question: What happend with the print when it
finish in a coma.

You have already been given this answer. You are passing two arguments
to the print function. The first argument is the string "\t$a". The
second argument is $a++. The print function is printing out both of
these arguments.

I have also given you the way to do this correctly:
for ($a=0; $a<$max; $a++) {
print "\t$a";
}

I continue to ask you *why* you think you want to put the 'print
"\t$a"' part of this code within the for loop header instead of in the
for loop block, where it belongs.
And where I can find quickly answers in Perl sintaxis, I
use Suse Linux full documentation installed.

I'm pretty sure the word you meant was "syntax". For that, look in the
documentation:
perldoc perlsyn

Paul Lalli
 
M

Mr. Crowley

Paul Lalli said:
I have asked you once already to post your reply *below* the text to
which you are replying. If you continue to be rude by not following
this convention, people will be far less likely to assist you.

<quoting fixed below. Again.>


This is the correct group. You just have to follow some conventions
and make an attempt at explaining yourself.



Yes, I understand that's what you're doing. I still don't understand
*why* you want to do that. The standard way to write a for loop is
for (INIT; COND; INCR) {
STATEMENTS;
}

For some reason, you keep trying to put the statement along with the
incrementation. WHY?

Why are you insistant on having a statement within the incrementation,
and have the block itself be completely empty?


You have already been given this answer. You are passing two arguments
to the print function. The first argument is the string "\t$a". The
second argument is $a++. The print function is printing out both of
these arguments.

I have also given you the way to do this correctly:
for ($a=0; $a<$max; $a++) {
print "\t$a";
}

I continue to ask you *why* you think you want to put the 'print
"\t$a"' part of this code within the for loop header instead of in the
for loop block, where it belongs.


I'm pretty sure the word you meant was "syntax". For that, look in the
documentation:
perldoc perlsyn

Paul Lalli

Thank you. Now I really understand.
But I don't know WHY I was doing that (put an empty {} after the 'for')
Just to try... in my Perl Book (Allen Wyke and Donald B. Thomas) say that
you can put more than three things in a for, but the problem was that my
fourth argument never existed, cause' $a++ is part of the print in this
case.

Sorry for this!!!!
 
P

Paul Lalli

Mr. Crowley said:
Thank you. Now I really understand.

Thank you for fixing your quoting style. Now you can fix it even
further by trimming down the quoted material to only the parts that are
relevant to your response. :)
But I don't know WHY I was doing that (put an empty {} after the 'for')
Just to try... in my Perl Book (Allen Wyke and Donald B. Thomas) say that
you can put more than three things in a for,

I don't know what book you're referring to, but I would suggest
throwing away any book that recommends or even suggests doing that.

For *good* Perl books, please see
perldoc -q book
and
perldoc perlbook
but the problem was that my
fourth argument never existed, cause' $a++ is part of the print in this
case.

That is a correct analysis.

Paul Lalli
 
D

Dave Weaver

for ($a=0; $a<$max; print "\t$a", $a++) {}
// OK, print 1 2 3 4 5 ... max

But

for ($a=0; $a<$max; $a++, print "\t$a") {}

// print 00 11 22 33 44... max-1max-1 ?

Others have pointed out why you get wrong output.

If you've put the print in that position so that it will be executed
at the end every loop regardless of 'next's, you should instead use a
continue block:

for my $i (1 .. $max) {
# ...
}
continue {
print "\$i";
}

see "perldoc -f continue"
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top