home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5537 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  2.3 KB

  1. Path: sparky!uunet!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Why is this foreach loop exiting early?
  5. Message-ID: <1992Aug27.062434.4867@netlabs.com>
  6. Date: 27 Aug 92 06:24:34 GMT
  7. References: <BtM6E1.81z@well.sf.ca.us> <123@eiffel.eiffel.com>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 61
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <123@eiffel.eiffel.com> ram@eiffel.com (Raphael Manfredi) writes:
  14. : Quoting nlane@well.sf.ca.us (Nathan D. Lane) from comp.lang.perl:
  15.  
  16. You mean, Diana Lane, not Nathan.  Tsk, tsk...   :-)
  17.  
  18. : >chop(@farms = `cat $file`);
  19. : That is your problem. That is a "shell" behaviour. What you want is more:
  20.  
  21. Nope, guess again.  chop(@foo = `cat $file`) does just what the lady expects.
  22.  
  23. : If the file is really big, it might be better (for memory usage) to write:
  24. : open(FARMS, $file) || die "Can't open $file: $!\n";
  25. : @farms = <FARMS>;    # Because $/ is "\n" and we are in array context
  26. : close FARMS;
  27.  
  28. If you're simply going to iterate over it you might as well use:
  29.  
  30.     while (<FARMS>) {
  31.     ...
  32.     }
  33.  
  34. : >The program doesn't reach the second record, or at least doesn't process it.
  35. : >Needless to say, it doesn't find the INDEX line either (which is why I
  36. : >haven't gotten that part of the processing to function).  Any help would be
  37. : >appreciated, including suggestions on style.  I do want to learn!
  38. : So no wonder, it did not work, you were trying to process one big line,
  39. : and $* is 0 by default so you did not match anything I guess.
  40.  
  41. Mmm.  No.
  42.  
  43. : Oh, BTW, I've just spot what might be a typo... Further down in your script,
  44. : you write:
  45. :     if ($farm =~ /INDEX/) {
  46. :         next until /\d+/;
  47. :         ($Seq, $rest) = split(' ', $farm);
  48. :     }
  49. : Surely you meant 'unless' instead of 'until'.
  50.  
  51. Yes, surely.
  52.  
  53. I don't know what the real problem is, but the way to solve it is to
  54. put print statements in to print out the value of things like $abbrev
  55. and $Farm{$seq} at appropriate places.  A nice trick is
  56.  
  57.     warn $abbrev if $DEBUGGING;
  58.  
  59. Then it automatically annotates it with the line number for you.
  60. Nothin' says that warn really has to produce a warning...
  61.  
  62. If one of those variables weren't getting set the second time then the
  63. program might just be appending more newlines to the first output file,
  64. or some such.
  65.  
  66. You might also try perl -d to step through it.
  67.  
  68. Larry
  69.