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