Prev: piped open and shell metacharacters
Next: FAQ 8.27 What's wrong with using backticks in a void context?
From: Sherm Pendley on 31 Jul 2010 11:46 "Thomas Andersson" <thomas(a)tifozi.net> writes: > Sherm Pendley wrote: > >> That being the case, you could use the index() function to see >> if $endstring appears anywhere in $page: >> >> if ( index($page, $endstring) == -1 ) { >> last; >> } > > I'm currently using: > last if $page =~/No sorties/; > Which seems to do the trick, is there a downside to using my solution? Not really, either will work just as well. sherm-- -- Sherm Pendley <www.shermpendley.com> <www.camelbones.org> Cocoa Developer
From: Thomas Andersson on 31 Jul 2010 11:55 Sherm Pendley wrote: > Which form to use is best judged on a case-by-case basis, with the > goal being readability. Ok, in my case I have two conditions and I want to end loop in different ways for each so I think the last version will work for me (If empty page exit before storing it, if it contains the last processed I still need to store it so do that and then exit). Your advice and examples really help and is appreciated! Best Wishes Thomas
From: sln on 31 Jul 2010 12:35 On Sat, 31 Jul 2010 17:28:18 +0200, "Thomas Andersson" <thomas(a)tifozi.net> wrote: >J�rgen Exner wrote: > >> Why does this remind me of the typical poor approaches of first year >> Computer Science students? :-) > >Well, I'm a one day hobbie studier so same same ;) > >> No, this is almost always a Very Bad Idea(TM). Setting flags like that >> quickly leads to very hard to maintain code. > >OK, good to know. > >> I have not idea what your exit criterion is, but you should loop while >> it is not met >> while (!exit_criterion(whateverArgYouNeedToComputeIt) >> >> Perl also gives you an additional function "last" which will exit the >> loop immediately. It is a nice pragmatic shortcut, although >> programming purists frown upon it. > >The loop has been rewritten and workds as intended now, using last to exit >on the two possible conditions. Now look like this: > >while (1) { > my $page = get "$pbase?page=$pcnt&pid=$pid"; > last if $page =~/No sorties/; > # Store grabbed webpage into the file > append_file( "c:/scr/$pid.txt", $page ) ; > last if $page =~/"sid=$lproc"/; > # Update page number and grab next. > $pcnt++; >}; > my $sid_rx = qr/sid=$lproc/i; for my $pid (1 .. 4) { my $fname = "c:/scr/$pid.txt"; open my $FHpid, ">>", $fname or die "Can't open $fname: $!"; my $pnumb = 1; while (defined( my $page = get( "$pbase?page=$pnumb&pid=$pid")) and $page !~ /No sorties/i ) { # Store webpage print $FHpid $page,"\n"; last if $page =~ /$sid_rx/; # Update page number, get next. $pnumb++; } close $FHpid; } ------------------ Beware that if $page is generated html, using a regex on it as in $page !~ /No sorties/i $page =~ /$sid_rx/ can be done, but only after it is parsed. It can be parsed with regex's ... though, thats beyond the scope of this post and another thing entirely. But, if you don't care, then its ok. -sln
From: J�rgen Exner on 31 Jul 2010 12:42 "Thomas Andersson" <thomas(a)tifozi.net> wrote: >J�rgen Exner wrote: >The loop has been rewritten and workds as intended now, using last to exit >on the two possible conditions. Now look like this: > >while (1) { Ouch, this hurts! Usually this line indicates a deamon which is never supposed to terminate. > my $page = get "$pbase?page=$pcnt&pid=$pid"; > last if $page =~/No sorties/; > # Store grabbed webpage into the file > append_file( "c:/scr/$pid.txt", $page ) ; > last if $page =~/"sid=$lproc"/; > # Update page number and grab next. > $pcnt++; >}; Why not move the loop condition into the loop condition? my $page = get "$pbase?page=$pcnt&pid=$pid"; while ((!$page =~/No sorties/) and (!$page =~/"sid=$lproc"/)) { append_file( "c:/scr/$pid.txt", $page ); $pcnt++; $page = get "$pbase?page=$pcnt&pid=$pid";} } Yes, I know the condition could be formulated better, but I transformed it as little as possible to demonstrate how the exit() cond can trivially be moved into the while() cond. BTW: space characters are very cheap, I just saw a them on sale at Costco. Fell free to use as many as you like to make your code more readable. jue
From: Uri Guttman on 31 Jul 2010 13:50
>>>>> "JE" == J�rgen Exner <jurgenex(a)hotmail.com> writes: >> while (1) { JE> Ouch, this hurts! Usually this line indicates a deamon which is never JE> supposed to terminate. not in my book. it just means an infinite loop. check inside for calls to last. fine with me and remember, i told him to do this! :) uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- |