Dear Monks,
I am looking for the best structure to create to manipulate some data.
I have several SQL statements in @statements to execute. I use the fetchrow_hashref to get rows. (The results of the previous statement will be used to execute the next request). The first statement returns only one row (hash_ref), and the others one can return one or many.
So, I need to access easily in this structure the field name (key of the hash_ref), the value and the number of rows sent by the statement.
I would like to handle all the results in a unique structure. I am thinking to this kind of hash
my %struture = (
'1' => {
'field1_statement1' => 'value',
'field2_statement1' => 'value'
},
'2.1' => {
'field1_statement2' => 'value',
'field2_statement2' => 'value',
'field3_statement2' => 'value'
},
'2.2' => {
'field1_statement2' => 'value',
'field2_statement2' => '',
'field3_statement2' => 'value'
},
'3.1' => {
'field1_statement2' => 'value'
}
);
and this is the code to generate it
my @statements;
my %structure;
foreach my $elt ( @statements ) {
my @ex = exec($elt);
my $i;
for (my $y=0;$y<=$#ex;$y++) {
my %results_temp = %{$ex[$y]};
my $index = "$i.$y";
foreach my $key (keys %results_temp)
{
$structure{$index}{$key} = $results_temp{$key};
}
}
}
sub exec {
my @param = @_;
my ($type, $db, $login, $pwd, $ip) = getDBInfo();
my $dbh = connectDB ($type, $db, $login, $pwd, $ip);
my @store;
foreach (@param) {
my $sth = $dbh->prepare ($_);
$sth->execute() || die $sth->errstr;
while (my $hash_ref = $sth->fetchrow_hashref) {
push @store, $hash_ref;
}
}
return @store;
}
What do you think? Is it relevant?
I don’t think it is efficient to count the number of 2.x. Either I calculate it on the fly or I store it in the structure, I don’t know…
Obviuously, I need an index to not override data.
Thanks in advance
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.