C
ccc31807
I'm processing a SQL file, and want to stuff the data into arrays that
will substitute for the database tables. I want each array element
element to be a reference to a hash. The index of the array will be my
'key', and the hash will represent the columns with the associated
values. In the real database, the key is a serial value which is
exactly the behavior of the array.
Since the SQL file will vary, I don't want to hard code the table
names, but dynamically create the arrays from the file. I've copied a
sample of the file below (from 'Beginning Databases with PostgreSQL'
by Matthew and Stoned), and the subroutine I'm using to process the
file. I create the $table, $keys, and $values for each row. I can use
a hash slice to create the hashes, but how do I create the appropriate
array (named after the table name)?
__CODE__
sub process_infile
{
open INFILE, '<', 'pop-all-tables.sql' or die "Cannot process
INFILE, $!";
while (<INFILE>)
{
next unless /^insert/ ;
chomp;
$_ =~ /^insert\s+into\s+ # match and skip 'insert into '
([^(]+)\( # match and save any word
character before the first open parens as $1
([^)]+)\) # match and save any word
character between the first set of parens as $2
\ values\( # match and skip ' values'
([^)]+)\) # match and save any word
character between the second set of parens as $3
/x;
my $table = $1;
my $keys = $2;
my $values = $3;
my @keys = parse_line(',', 0, $keys);
my @values = parse_line(',', 0, $values);
#print " TABLE $table: KEYS: [@keys] => VALUES: [@values]
\n";
my %hash;
@hash{@keys} = @values;
#foreach my $key (keys %hash) { print "$key => $hash{$key}
\n"; }
my $test = eval($table);
print "test is $test\n";
#push @{eval($table)}, \%hash;
}
close INFILE;
}
__SQL__
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Miss','Jenny','Stones','27 Rowan
Avenue','Hightown','NT2 1AQ','023 9876');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Andrew','Stones','52 The Willows','Lowtown','LT5
7RA','876 3527');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Miss','Alex','Matthew','4 The Street','Nicetown','NT2
2TX','010 4567');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Adrian','Matthew','The Barn','Yuleville','YV67
2WR','487 3871');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Simon','Cozens','7 Shady Lane','Oakenham','OA3
6QW','514 5926');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Neil','Matthew','5 Pasture Lane','Nicetown','NT3
7RT','267 1232');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Richard','Stones','34 Holly Way','Bingham','BG4
2WE','342 5982');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Ann','Stones','34 Holly Way','Bingham','BG4
2WE','342 5982');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Christine','Hickman','36 Queen
Street','Histon','HT3 5EM','342 5432');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Mike','Howard','86 Dysart Street','Tibsville','TB3
7FG','505 5482');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Dave','Jones','54 Vale Rise','Bingham','BG3
8GD','342 8264');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Richard','Neill','42 Thatched
Way','Winnersby','WB3 6GQ','505 6482');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Laura','Hardy','73 Margarita Way','Oxbridge','OX2
3HX','821 2335');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Bill','O\'Neill','2 Beamer Street','Welltown','WT3
8GM','435 1234');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','David','Hudson','4 The Square','Milltown','MT2
6RT','961 4526');
will substitute for the database tables. I want each array element
element to be a reference to a hash. The index of the array will be my
'key', and the hash will represent the columns with the associated
values. In the real database, the key is a serial value which is
exactly the behavior of the array.
Since the SQL file will vary, I don't want to hard code the table
names, but dynamically create the arrays from the file. I've copied a
sample of the file below (from 'Beginning Databases with PostgreSQL'
by Matthew and Stoned), and the subroutine I'm using to process the
file. I create the $table, $keys, and $values for each row. I can use
a hash slice to create the hashes, but how do I create the appropriate
array (named after the table name)?
__CODE__
sub process_infile
{
open INFILE, '<', 'pop-all-tables.sql' or die "Cannot process
INFILE, $!";
while (<INFILE>)
{
next unless /^insert/ ;
chomp;
$_ =~ /^insert\s+into\s+ # match and skip 'insert into '
([^(]+)\( # match and save any word
character before the first open parens as $1
([^)]+)\) # match and save any word
character between the first set of parens as $2
\ values\( # match and skip ' values'
([^)]+)\) # match and save any word
character between the second set of parens as $3
/x;
my $table = $1;
my $keys = $2;
my $values = $3;
my @keys = parse_line(',', 0, $keys);
my @values = parse_line(',', 0, $values);
#print " TABLE $table: KEYS: [@keys] => VALUES: [@values]
\n";
my %hash;
@hash{@keys} = @values;
#foreach my $key (keys %hash) { print "$key => $hash{$key}
\n"; }
my $test = eval($table);
print "test is $test\n";
#push @{eval($table)}, \%hash;
}
close INFILE;
}
__SQL__
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Miss','Jenny','Stones','27 Rowan
Avenue','Hightown','NT2 1AQ','023 9876');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Andrew','Stones','52 The Willows','Lowtown','LT5
7RA','876 3527');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Miss','Alex','Matthew','4 The Street','Nicetown','NT2
2TX','010 4567');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Adrian','Matthew','The Barn','Yuleville','YV67
2WR','487 3871');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Simon','Cozens','7 Shady Lane','Oakenham','OA3
6QW','514 5926');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Neil','Matthew','5 Pasture Lane','Nicetown','NT3
7RT','267 1232');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Richard','Stones','34 Holly Way','Bingham','BG4
2WE','342 5982');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Ann','Stones','34 Holly Way','Bingham','BG4
2WE','342 5982');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Christine','Hickman','36 Queen
Street','Histon','HT3 5EM','342 5432');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Mike','Howard','86 Dysart Street','Tibsville','TB3
7FG','505 5482');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Dave','Jones','54 Vale Rise','Bingham','BG3
8GD','342 8264');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Richard','Neill','42 Thatched
Way','Winnersby','WB3 6GQ','505 6482');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mrs','Laura','Hardy','73 Margarita Way','Oxbridge','OX2
3HX','821 2335');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','Bill','O\'Neill','2 Beamer Street','Welltown','WT3
8GM','435 1234');
insert into customer(title, fname, lname, addressline, town, zipcode,
phone) values('Mr','David','Hudson','4 The Square','Milltown','MT2
6RT','961 4526');