in reply to Recommendations for breaking up string?
I changed the DB schema - suggest you rethink yours. Anytime there are column names FOO1 FOO2 FOO3 that's a red flag of an awkward schema, however SQL Schema theory is beyond the scope of this reply.
#!/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 J +ohn 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}); }
Here's the DB as dumped by sqlite db.1142225 .dump
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;
DB Hint: come up with the several ways you will query the DB and that might help your DB schema (sample query: "get all RB". This is slightly awkward with this schema, how is it with the RB1 RB2 column name schema? )
|
|---|