in reply to Process Text File and Write to Database

Your second while loop is effectively:
open INPUT,'<',$out_file or die "Can't open file " . $out_file . "\n$! +\n"; #Open for read while (<INPUT>) { my $name = $connect->quote($_); next; }

See next

Replies are listed 'Best First'.
Re^2: Process Text File and Write to Database
by spickles (Scribe) on Nov 20, 2009 at 19:04 UTC

    I call the execute at the bottom. It looks like I don't understand the use of 'next' in this context, so I'm open to other suggestions as to how to read that file line by line and add the records to the database. When I reach a line that contains the word 'profit' or 'Government', that is the end of a record. Within a record, I need to skip lines that match 'Councils', 'Continuing', and 'Mapping' as these are irrelevant lines of data. The problem is that not all records contain all three of those lines, so I can't just arbitrarily increase a counter. I was having a good deal of success reading the file to an array and then using a 'for' loop, but I stumble across the cases where I need to toss out those irrelevant lines.

      It looks like I don't understand the use of 'next' in this context,
      Yes. 'next' means "exit the block I'm in now, and skip anything (other than a continue block) afterwards." Every time you called 'next', your program skipped directly to the next iteration of the loop without going any further - in your original program, that's why only the
      my $name = $connect->quote($_); next;
      parts of the loop were being executed.
      This is quick, ugly, and prone to fail at the slightest change in the data, but maybe it'll get you started:
      open INPUT,'<',$out_file or die "Can't open file " . $out_file . "\n$! +\n"; #Open for read while (<INPUT>) { my $name = $connect->quote($_); my $address1 = $connect->quote(<INPUT>); my $address2 = $connect->quote(<INPUT>); my $phone = $connect->quote(<INPUT>); for (<INPUT>) { next if /(Council|^Continuing|^Mapping)/; last; } my $overall = $connect->quote($_); my $inspections = $connect->quote(<INPUT>); my $staffing = $connect->quote(<INPUT>); my $quality = $connect->quote(<INPUT>); my $programs = $connect->quote(<INPUT>); my $beds = $connect->quote(<INPUT>); my $ownership = $connect->quote(<INPUT>); my $query_string = "INSERT INTO nursing_homes (name, address1, + address2, phone, overall, inspections, staffing, quality, programs, +beds, ownership) VALUES ($name, $address1, $address2, $phone, $overal +l, $inspections, $staffing, $quality, $programs, $beds, $ownership)"; #printVariables($name, $address1, $address2, $phone, $overall, + $inspections, $staffing, $quality, $programs, $beds, $ownership, $qu +ery_string); my $query_handle = $connect->prepare("INSERT INTO nursing_home +s (name, address1, address2, phone, overall, inspections, staffing, q +uality, programs, beds, ownership) VALUES ($name, $address1, $address +2, $phone, $overall, $inspections, $staffing, $quality, $programs, $b +eds, $ownership)"); $query_handle->execute(); }

      You define $query_string but don't use it, and you're not checking return values for the prepare and execute calls. You must.