coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use a loop but had no luck getting it to work as expected.
my $cnt = 0; do { $cnt++ print "We are on loop # $cnt"; print "$dynamic"; } until ($cnt == "10")
I'm trying to get it to loop over everything (and do EVERYTHING) until $cnt == #. $dynamic is a variable that WILL change every time it runs through the loop so what I'm trying to do is get it to print out before the next loop starts so it doesn't overwrite itself. That way if I print "this is loop # $cnt;" all the data for that one loop iteration will be under it so I can later tell which data came from which loop.

Any suggestions?

Replies are listed 'Best First'.
Re: Which loop should I use?
by The Mad Hatter (Priest) on Jul 31, 2003 at 18:38 UTC
    Even more succintly
    my $count = 10; for (0..$count) { print "We are on loop # $_\n"; }
Re: Which loop should I use?
by pjanzen (Sexton) on Jul 31, 2003 at 18:34 UTC
    i guess i would do it like this .... hope this helps Paul
    #!/usr/bin/perl -w use strict; my $count = 10; for(my $i=0;$i<$count;$i++) { print "Dollar count = " . $i . "\n"; }
    The output is
    
    fox_m@illegal:~$ ./quick.pl
    Dollar count = 0
    Dollar count = 1
    Dollar count = 2
    Dollar count = 3
    Dollar count = 4
    Dollar count = 5
    Dollar count = 6
    Dollar count = 7
    Dollar count = 8
    Dollar count = 9
    
Re: Which loop should I use?
by Mr. Muskrat (Canon) on Jul 31, 2003 at 18:40 UTC

    # $dynamic declared prior my $start = 0; my $end = 10; for my $cnt ($start .. $end) { # for my $cnt (0..10) { print "We are on loop # $cnt\n"; print "$dynamic\n"; }

    Update: Once again, I am too slow. If this keeps up, you can start calling me Slowpoke Rodriguez!

      Can you tell me what's wrong with
      my $start = 1; my $end = 10; my $cnt = 0; for ($start..$end) { $cnt++; print "<b>Current page count: $cnt</b><p><p>"; my $funky = "http://www.allpoetry.com/chat/page=$cnt;"; my $content = get($funky); my $tree = HTML::Tree->new(); $tree->parse($content); # retrieve the text and split into lines my @lines = split "<br>", $tree->as_text; local $/; my @good_lines; my $good_lines; for my $lines (@lines) { $lines =~ s/\)/\)<br>/g; while($lines =~ m/Next Chatter \>(.*?)\< Previous Chatter/gs){ $good_lines = $1; push @good_lines,$good_lines; } foreach (@good_lines){ my @lines = split /<br>/; foreach (@lines){ next unless $_; #m/^(.*?):(.*?) \((.*) (?:seconds|minute|minutes|hour|hours|day +|days) ago\)$/; #m/([^:]+): (.+)\((.*)\)/; m/([^:]+): (.+)(\((.*)\))/; my( $name, $text, $delay ) = ( $1, $2, $3 ); #print "NAME:$name\nText:$text\nDelay:$delay\n\n"; #if ($name =~ m/Elisabeth/) { print "$name $text $delay<br>"; # $text $delay<br>" #} } } } }
      ? I'm doing what you showed as an example and I get:
      sprkls926 hello (17 minutes ago) sprkls926 i don't feel good (17 minutes ago) kinkygoddess hiya ppls (17 minutes ago) sprkls926 *faints and slids off the roof* (17 minutes ago) sprkls926 *watches the blood follow outta her* (18 minutes ago) Shinigami why sad ? (18 minutes ago) Shinigami why sad ? (18 minutes ago) sprkls926 sad (19 minutes ago) sprkls926 *crys and cuts her neck* (19 minutes ago) Shinigami so howz everyone ? (19 minutes ago) FiXato sure sw...just comment on mine and I will get back to one of yo +urs right away (20 minutes ago) sidewinder hmmm.....not that I know of... (20 minutes ago) sprkls926 sidewinder is there anyway to revires this vampire thing* + (21 minutes ago) Shinigami hello everyone (22 minutes ago) sidewinder No one wants to trade? (22 minutes ago) vampira1665 morning sidewinder, I will read some of ur stuff if there +is any I haven't read (22 minutes ago) sidewinder no wants to trade??? (22 minutes ago) sprkls926 *starts to claw at her neck* (24 minutes ago) sprkls926 *backs away from everyone* (25 minutes ago) sidewinder anyone (25 minutes ago) ForgottenAngel666 ::stays in my corner alone:: (25 minutes ago) sidewinder anywanna trade comments? (26 minutes ago) sidewinder anywanna trade comments? (26 minutes ago) Yume *sighs* I must leave but I will return! (26 minutes ago) Yume *sighs* I must leave but I will return! (26 minutes ago) Yume *sighs* I must leave but I will return! (27 minutes ago) sidewinder joins sprkls (29 minutes ago) sprkls926 *sits on the roof with her head cast downward* (30 minut +es ago) sprkls926 *bites sidewinder then leaves the room* (30 minutes ago) sidewinder *smiles* (31 minutes ago) Yume noooooooooo! not monkey!! (32 minutes ago) sprkls926 munkey come back i am going outside u r safe no one will hur +t u (32 minutes ago) sprkls926 i will be back soon (32 minutes ago) sathethert with that, i have to go to trauma. see yaaaaaaaaaaaaaa +(32 minutes ago) ForgottenAngel666 ..munkey come back.! (not that u even like me no mor +e ) ForgottenAngel666 ..munkey come back.! (not that u even like me no mor +e ) ForgottenAngel666 ..munkey come back.! (not that u even like me no mor +e ) Current page count: 2 Current page count: 3 Current page count: 4 Current page count: 5 Current page count: 6 Current page count: 7 Current page count: 8 Current page count: 9 Current page count: 10

        There's nothing wrong with your loop.. but the HTML on the first page is different from the rest, you're just not parsing it correctly.The first page doesn't have a - between the < and 'Next Chatter' which throws off your regex.

        Well I got interested in this problem, So here's a replacement :). I dropped HTML::Tree because I thought it would be nice to be able to have the full text of messages and it was much simpler to just grab straight from the HTML. It should be trivial to run the message text back through HTML::Tree to strip the HTML. I had originally tried tackling this using the parse tree but the code was twice as long and uglier, not to mention it didn't work :(. I created a get function that grabbed off local disk so I didn't have to hit the website whenever I wanted to test.

        And on to the code!:

        Update: Everything that isn't struck out :)

        Update2: Added a few linebreaks so one line of code wouldn't wrap

        I'd really recommend using something more standard. For example:
        for my $cnt ($start .. $end) { # ... }
        There's no need to have an independent $cnt when you can use that name as the looping variable.

        Also, what's wrong with that output? Maybe you could check that the page is loading properly, for example, by testing defined($content) && length($content)
Re: Which loop should I use?
by tadman (Prior) on Jul 31, 2003 at 19:16 UTC
    Keep in mind that sometimes your output is not displayed right away because you didn't finish upwith a linefeed. Almost all print statements should finish with one, for example:
    print "This is my comment\n";
    The "\n" will send the line to the terminal immediately. If you don't have a trailing linefeed, the line might get buffered, which can delay its appearance.