my $do = 1; for (0...9) { stuff_to_do_always(); if ($do) { stuff_to_do_first_time(); } else { stuff_to_do_other_times(); } $do = 0; } #### use DBI; my $dbh = DBI->connect("DBI:mysql:database=chess", "usr", "pwd"); # or whatever my @line; my $sth; while () { # your parsing: chomp; next if /^\s*$/; @line = split /;/; # these lines deal with the only variation in # your data I can see - there may be others: if (@line == 3) { push @line, $line[2]; $line[2] = 'NULL'; } # and finally, DBI takes the strain: $sth = $dbh->prepare("INSERT INTO eco_class (eco_id,eco,opening,variation,moves) VALUES (NULL,?,?,?,?)") or die $dbh->errstr; $sth->execute(@line) or die $dbh->errstr; }