home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5521 < prev    next >
Encoding:
Text File  |  1992-08-26  |  3.1 KB  |  82 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!sun-barr!ames!pacbell.com!well!nlane
  3. From: nlane@well.sf.ca.us (Nathan D. Lane)
  4. Subject: Why is this foreach loop exiting early?
  5. Message-ID: <BtM6E1.81z@well.sf.ca.us>
  6. Sender: news@well.sf.ca.us
  7. Organization: Whole Earth 'Lectronic Link
  8. Date: Wed, 26 Aug 1992 23:25:13 GMT
  9. Lines: 71
  10.  
  11. Hello all,
  12.     I was really hoping I could write this one "all by myself" but I seem to
  13. be stuck.  If I had a few more days (I've already spent four or five days on
  14. this) I might stumble on the answer, but...  Anyway, first things first:
  15.  
  16. seldon{diana}1243$ perl -v
  17. This is perl, version 4.0
  18.  
  19. $RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $
  20. Patch level: 19
  21.  
  22. I am trying to write a program that will go through a file approximately 20K
  23. in size, find a line beginning with 'MAIL:' in the middle of each of 25 or
  24. so records, do all sorts of nonsense to find out what county the mailing
  25. address is in, create a file named for the county, write the record to the
  26. file, then go on to the next record.  The "nonsense" (the process to find
  27. the county) works fine.  The first record winds up in a file named for the 
  28. correct county. The problem is that the program quietly exits after closing
  29. the file it has just written to.  Maybe some code will help - I'll keep it
  30. as short as possible to conserve bandwidth and your patience.
  31.  
  32. #!/usr/bin/perl
  33.  
  34. ($file) = @ARGV;
  35. chop(@farms = `cat $file`);
  36. print "farms read in...\n";       # data file
  37. chop(@zips = `cat /u/src/data/zip/zipcode.txt`);
  38. print "zips read in...\n";       # file contains zip:city, state
  39. chop(@counties = `cat /home/diana/prg/Trico/cs/cities.txt`);
  40. print "counties read in...\n";    # file contains city:county
  41.  
  42. $i = 0;
  43. $j = 1;
  44. foreach $farm (@farms) {
  45.         next unless ($farm =~ /SEQ/);    # I have also tried without the next
  46.         if ($farm =~ /SEQ/) {
  47.                 ($junk, $seq) = split(':', $farm);
  48.                 ($seq, $junk) = split('\-', $seq);
  49.                 until ($farms[$j] =~ /SEQ|SEARCH COMPLETE/ ) {
  50.                         $Farm{$seq} = join("\n", $Farm{$seq}, $farms[$i]);
  51.                         if ($farms[$i] =~ /MAIL/) {
  52.                                 &get_cty;
  53.                         }
  54.                         $i++;
  55.                         $j++;
  56.                 }
  57.                 open(COUNTY, ">>counties/$abbrev") || die "Can't create
  58. $abbrev: $!\n";
  59.                 print COUNTY "$Farm{$seq}\n";
  60.                 close COUNTY;
  61.         }
  62.  
  63.         # This part doesn't work yet; when it does it will
  64.         # take the sequence number ($Seq) and put the current
  65.         # line ($farm) into the file containing the corresponding
  66.         # record ($Farm{$seq} above).  Any tips welcome.
  67.  
  68.         if ($farm =~ /INDEX/) {        # comes after the 'SEARCH COMPLETE' line
  69.                 next until /\d+/;
  70.                 ($Seq, $rest) = split(' ', $farm);
  71.         }
  72. }
  73.  
  74. The program doesn't reach the second record, or at least doesn't process it.
  75. Needless to say, it doesn't find the INDEX line either (which is why I
  76. haven't gotten that part of the processing to function).  Any help would be
  77. appreciated, including suggestions on style.  I do want to learn!
  78.  
  79. - Diana B. Lane
  80. nlane@well.sf.ca.us
  81. Trico Title Co., Santa Barbara, CA
  82.