in reply to Re^2: Creating a random generator
in thread Creating a random generator

So I had some free time, and I haven't written any code for fun in a while, so I decided to put my money where my mouth is. Here's the skeleton of a solution for your proficiency tables. I created a mysql database named 'player', and create one table called 'proficiencies':
mysql> describe proficiencies; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | proficiency | varchar(50) | NO | | | | | s | int(11) | YES | | NULL | | | ra | varchar(20) | NO | | | | | cm | int(11) | YES | | NULL | | | class | varchar(30) | YES | | NULL | | | source | varchar(30) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+
I scraped your a-c table and turned it into a text file delimited by pipe chars (since there are commas in the data):
|Acting|1|Cha|0|Bd, Hu| |Adaptation|2|Int|0|Wa 10+, R 11+|HL| |Administration|1|Int|1|Pr|SM|
I wrote a script to stuff the data into the db:
#!/usr/bin/perl use strict; use DBI; use DBD::mysql; my $dsn = "DBI:mysql:database=player;"; my $dbh = DBI->connect($dsn, "root", "secretpassword"); open IN, "<table.txt" or die "Error - failed to open file for reading: + $!\n"; while ( <IN> ) { chomp; my ($null,$prof,$s,$ra,$cm,$class,$source) = split /\|/; my $sql =<<SQLDONE ; INSERT INTO proficiencies VALUES ( '$prof', $s, '$ra', $cm, '$class', +'$source' ); SQLDONE print "$sql\n"; my $sth = $dbh->prepare($sql); $sth->execute; } print "finished loading table\n";
That was probably not the most efficient way to do it, but my dbi book is at work and all this is off the top of my head.

Script to read the database and dump data into template toolkit template:

#!/usr/bin/perl use strict; use DBI; use DBD::mysql; use Template; my $tt = Template->new(); my $dsn = "DBI:mysql:database=player;"; my $dbh = DBI->connect($dsn, "root", "secretpassword"); my @items; my $sql = qq|SELECT * from proficiencies ORDER BY proficiency ASC;|; my $sth = $dbh->prepare($sql); $sth->execute; while ( my @row = $sth->fetchrow_array() ) { push @items, \@row; } my $vars = { items => \@items }; print "Content-type: text/html\n\n"; $tt->process("prof.tmpl", $vars) or die $tt->error();
Template toolkit template to display the data:
<html> <head><title>Proficiencies</title> <body> <table border="1" cellspacing="2" cellpadding="2"> <tr> <th>Proficiency</th> <th>S</th> <th>RA</th> <th>CM</th> <th>Class(es)</th> <th>Source(s)</th> </tr> [% FOREACH row IN items %] <tr> <td align="left">[% row.0 %]</td> <td align="center">[% row.1 %]</td> <td align="center">[% row.2 %]</td> <td align="center">[% row.3 %]</td> <td align="left">[% row.4 ? row.4 : "&nbsp;" %]</td> <td align="left">[% row.5 %]</td> </tr> [% END %] </table> </body> </html>
Live script can be run by hitting this url: http://marold.org/cgi-bin/showtable.cgi

Replies are listed 'Best First'.
Re^4: Creating a random generator
by Lady_Aleena (Priest) on Oct 07, 2007 at 19:03 UTC

    UnstoppableDrew;

    Before this goes too much further, MySQL is unavailable to me. It looks great, but I would need it to be pure Perl. Also, other things will be happening once I get the table to print, like having select boxes where the users can choose which proficiencies that they want to see.

    Great work! Have a splendid day...

    Lady Aleena

    "An it harm none, do as ye will."