At the end of the code are queries for each of the 2 tables (showing all their contents).
Chris
#!/usr/bin/perl use strict; use warnings; use DBI; my @data; chomp(my @sets = split "\t", <DATA>); <DATA>; while (<DATA>) { my @xys = split; for my $dataset (0..@xys/2-1) { push @{ $data[$dataset] }, [splice @xys, 0, 2]; } } my $dbh = DBI->connect("dbi:SQLite:dbname=datapoints_01.lite","","") o +r die $!; $dbh->do(qq{ CREATE TABLE data_sets (id INTEGER, name TEXT) }); my $sth = $dbh->prepare(q{INSERT INTO data_sets VALUES(?, ?)}) or die +$dbh->errstr; my $id; for my $name (@sets) { $sth->execute(++$id, $name) or die $dbh->errstr; } $dbh->do(qq{ CREATE TABLE data_pts (set_id INTEGER, id INTEGER, x INTEGER, y INTEGER) }); $sth = $dbh->prepare(q{INSERT INTO data_pts VALUES(?, ?, ?, ?)}) or die $dbh->errstr; for my $set_id (0..$#data) { for my $point_id (0..$#{ $data[$set_id] }) { $sth->execute($set_id+1, $point_id+1, @{ $data[$set_id][$point +_id] }) or die $dbh->errstr; } } $sth->finish; $dbh->disconnect or die $dbh->errstr; __DATA__ Data Set 1 Data Set 2 Data Set 3 X units Y units X units Y units X units Y units 1 2 10 20 100 200 3 4 30 40 300 400 5 6 50 60 500 600 __END__ C:\perlp>sqlite3 datapoints_01.lite SQLite version 3.3.8 Enter ".help" for instructions sqlite> select * from data_sets; 1|Data Set 1 2|Data Set 2 3|Data Set 3 sqlite> select * from data_pts; 1|1|1|2 1|2|3|4 1|3|5|6 2|1|10|20 2|2|30|40 2|3|50|60 3|1|100|200 3|2|300|400 3|3|500|600 sqlite>
In reply to Re: Help munging tabular data
by Cristoforo
in thread Help munging tabular data
by c4onastick
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |