mistake in my for

Discussion in 'Perl Misc' started by Olaf \El BLanco\, Jan 20, 2006.

  1. 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 ?
     
    Olaf \El BLanco\, Jan 20, 2006
    #1
    1. Advertising

  2. 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
     
    Glenn Jackman, Jan 20, 2006
    #2
    1. Advertising

  3. 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?
     
    Brian McCauley, Jan 20, 2006
    #3
  4. 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?
     
    Olaf \El BLanco\, Jan 20, 2006
    #4
  5. Olaf \El BLanco\

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 20, 2006
    #5
  6. Olaf \El BLanco\

    Jim Cook Guest


    > 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?
     
    Jim Cook, Jan 20, 2006
    #6
  7. Olaf \El BLanco\

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 20, 2006
    #7
  8. Olaf \El BLanco\

    Guest

    "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
     
    , Jan 20, 2006
    #8
  9. 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:...
    > 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
    >
     
    Olaf \El Blanco\, Jan 21, 2006
    #9
  10. 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
     
    Tad McClellan, Jan 21, 2006
    #10
  11. Olaf \El BLanco\

    Paul Lalli Guest

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

    Olaf "El Blanco" wrote:
    > "Paul Lalli" <> escribió en el mensaje
    > news:...
    > > 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";
    > > }
    > >

    > 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
     
    Paul Lalli, Jan 21, 2006
    #11
  12. Olaf \El BLanco\

    Mr. Crowley Guest

    Paul Lalli <> el Sábado, 21 de Enero de 2006 15:11 wrote:

    > 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.>
    >
    > Olaf "El Blanco" wrote:
    >> "Paul Lalli" <> escribió en el mensaje
    >> news:...
    >> > 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";
    >> > }
    >> >

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


    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!!!!
     
    Mr. Crowley, Jan 21, 2006
    #12
  13. Olaf \El BLanco\

    Paul Lalli Guest

    Mr. Crowley wrote:

    > 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
     
    Paul Lalli, Jan 21, 2006
    #13
  14. Olaf \El BLanco\

    Dave Weaver Guest

    On Fri, 20 Jan 2006 21:30:03 +0100, 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 ?
    >


    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"
     
    Dave Weaver, Jan 27, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \(beta-\) Frank Nitzsche

    where is the mistake?

    \(beta-\) Frank Nitzsche, Jun 25, 2004, in forum: VHDL
    Replies:
    4
    Views:
    550
  2. Jesse

    Beginner Mistake

    Jesse, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Jesse
    Jul 22, 2003
  3. Joerg Jooss
    Replies:
    1
    Views:
    415
    DotNetJunkies User
    May 22, 2004
  4. John
    Replies:
    6
    Views:
    668
  5. Leon

    Does anyone see my mistake?

    Leon, Nov 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    343
Loading...

Share This Page