Prev: piped open and shell metacharacters
Next: FAQ 8.27 What's wrong with using backticks in a void context?
From: J�rgen Exner on 31 Jul 2010 10:57 "Thomas Andersson" <thomas(a)tifozi.net> wrote: >Next thing I need to add is a check for the exit >conditions. Thinking about using $page as condition might be a bad idea, how >about it checking for a signal variable to be set? Inside the loop code >would run untill my exit conditions are meet and then it sets the signal >variable telling the loop to end? Why does this remind me of the typical poor approaches of first year Computer Science students? :-) No, this is almost always a Very Bad Idea(TM). Setting flags like that quickly leads to very hard to maintain code. 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. jue
From: J�rgen Exner on 31 Jul 2010 11:02 "Thomas Andersson" <thomas(a)tifozi.net> wrote: >Prob is it will never fail, the server keeps feeding pages with no content >in so a test needs to be added inside the loop. Then obviously you are using the wrong condition for your loop. >Would the following code help exiting the loop? > > if ( $page eq $endstring ) { > exit( 0 ); > }; > >($endstring = "No more sorties" which is the string replacing data in emprty >pages). If this is the end condition for the loop then it would help even more if your put it in the condition for the loop. while (........ and ($page ne $endstring)) { jue
From: Thomas Andersson on 31 Jul 2010 11:28 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++; };
From: Thomas Andersson on 31 Jul 2010 11:33 Sherm Pendley wrote: > Exit() exits the *program*. In this case, since your loop is basically > the whole program, it amounts to the same thing, but that won't always > be the case! Better to use last - that's what it's for. It will make a difference as this is only part of the program I need to do. I'm using last now. > Is it sent as plain text, without even a newline character at the end? > I doubt that - more likely, it's an HTML page that *contains* that > string. 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?
From: Sherm Pendley on 31 Jul 2010 11:39
Jürgen Exner <jurgenex(a)hotmail.com> writes: > If this is the end condition for the loop then it would help even more > if your put it in the condition for the loop. > > while (........ and ($page ne $endstring)) { It's a judgement call. Sometimes, especially if there are several con- ditions, it can make more sense to use last: while(foo && bar && baz) while(1) { last unless foo; last unless bar; last unless baz; ... } Which form to use is best judged on a case-by-case basis, with the goal being readability. sherm-- -- Sherm Pendley <www.shermpendley.com> <www.camelbones.org> Cocoa Developer |