#!/usr/bin/perl # http://perlmonks.org/?node_id=1142225 use strict; use warnings; use DBI; $_ = 'QB Carson Palmer RB Chris Ivory RB Eddie Lacy WR A.J. Green WR John Brown WR Davante Adams TE Martellus Bennett FLEX Jeremy Hill DST Panthers'; my $dbfilename = 'db.1142225'; unlink $dbfilename; my $db = DBI->connect_cached("dbi:SQLite(RaiseError=>1):$dbfilename"); $db->do('create table players (team text, position text, player text)'); my %dups = /\b([A-Z]{2,})\b(?=.*\b(\1)\b)/g; # insert counts if needed for my $position (keys %dups) { my $n = 1; s/\b$position\K\b/$n++/ge; } # make hash my %db = /\b([A-Z]{2,}\d*) (.*?)(?= [A-Z]{2}|$)/g; #use Data::Dump 'pp'; pp \%db; # remove team (this is a guess) my $team = delete $db{'DST'} or die "no team"; for my $pos (keys %db) { $db->do('insert into players values (?, ?, ?)', {}, $team, $pos, $db{$pos}); } #### PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE players (team text, position text, player text); INSERT INTO "players" VALUES('Panthers','QB','Carson Palmer'); INSERT INTO "players" VALUES('Panthers','TE','Martellus Bennett'); INSERT INTO "players" VALUES('Panthers','WR2','John Brown'); INSERT INTO "players" VALUES('Panthers','RB1','Chris Ivory'); INSERT INTO "players" VALUES('Panthers','FLEX','Jeremy Hill'); INSERT INTO "players" VALUES('Panthers','WR3','Davante Adams'); INSERT INTO "players" VALUES('Panthers','WR1','A.J. Green'); INSERT INTO "players" VALUES('Panthers','RB2','Eddie Lacy'); COMMIT;