Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > mistake in my for

Reply
Thread Tools

mistake in my for

 
 
Olaf \El BLanco\
Guest
Posts: n/a
 
      01-20-2006


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 ?



 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      01-20-2006
At 2006-01-20 03:30PM, Olaf "El BLanco" <> wrote:
>
>
> 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.

--
Glenn Jackman
Ulterior Designer
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-20-2006

Olaf "El BLanco" wrote:
> 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?

 
Reply With Quote
 
Olaf \El BLanco\
Guest
Posts: n/a
 
      01-20-2006
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.

Brian McCauley <> el Viernes, 20 de Enero de 2006 21:46
wrote:

>
> Olaf "El BLanco" wrote:
>> 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?


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-20-2006
Please do not top-post. Post your reply *below* the text to which you
are replying, after trimming it appropriately.

<quoting fixed below>

Olaf El BLanco wrote:
> Brian McCauley <> wrote:
>
> > Olaf "El BLanco" wrote:


> >> 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?


> 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

 
Reply With Quote
 
Jim Cook
Guest
Posts: n/a
 
      01-20-2006

> 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?
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-20-2006
Jim Cook wrote:
> > 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.


> 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

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-20-2006
"Paul Lalli" <> wrote:
> Jim Cook wrote:
> > > 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.

>
> > 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?


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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      01-21-2006
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.




"Paul Lalli" <> escribió en el mensaje
news: oups.com...
> Please do not top-post. Post your reply *below* the text to which you
> are replying, after trimming it appropriately.
>
> <quoting fixed below>
>
> Olaf El BLanco wrote:
>> Brian McCauley <> wrote:
>>
>> > Olaf "El BLanco" wrote:

>
>> >> for ($a=0; $a<$max; print "\t$a", $a++) {}
>> >> // OK, print 1 2 3 4 5 ... max
>> >>
>> >> But
>> >>
>> >
>> > You have the code and the output swapped.
>> >
>> > Other than that, did you have a question?

>
>> 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
>



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-21-2006
Glenn Jackman <> wrote:
> At 2006-01-20 03:30PM, Olaf "El BLanco" <> wrote:
>>
>>
>> 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.



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.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Made a mistake Cisco router 3640 elliotth123 Cisco 0 09-22-2005 12:39 AM
New computer wireless PCI adapter setup mistake recovery =?Utf-8?B?Q3VyaW91c01hcms=?= Wireless Networking 3 02-15-2005 04:01 PM
Access-List: Blocking all access by mistake Sarah Cisco 4 11-30-2004 11:14 PM
where is the mistake? \(beta-\) Frank Nitzsche VHDL 4 06-25-2004 06:53 PM
erased flash by mistake, now not booting David Hodgson Cisco 3 03-02-2004 07:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57