#!/usr/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect( " ...whatever... " ); my @insert_fields = qw{ name address1 address2 phone overall inspections staffing quality programs beds ownership }; my $insert_sql = 'insert into nursing homes ('. join( ', ', @insert_fields ). ') values ('. join( ', ', ('?') x @insert_fields ). ')'; my $insert_sth = $dbh->prepare( $insert_sql ); $/ = ""; # set input_record_separator to empty string (paragraph mode) # just put the input file name on the command line when running the script # (or pipe the data to the script's STDIN) while (<>) # each iteration reads up to a blank line { my @lines = grep !/ Councils?$|^Mapping|^Continuing/, split( /[\r\n]+/ ); if ( @lines != @insert_fields ) { # skip records that won't work print "Record # $. has wrong number of fields:\n$_\n"; next; # if you redirect STDOUT to a file, you can deal with these later } $insert_sth->execute( @lines ); } $insert_sth->finish; $dbh->disconnect; #### @lines = grep !/^\s*$| Councils?$|^Mapping|^Continuing/, split( /[\r\n]+/ );