That's right, what you have is illegal syntax in either
language. You are trying to access a HoH (hash of hashes)
instead of what you really have, a LoH (list of hashes).
You simply are missing the array index:
genesis['Foxtrot'][0]['instrumental'] = 'excellent'
$gensis->{Foxtrot}[0]{instrumental} = 'excellent';
Now then, as for populating these datastructures from a
database ... well, i can't help you much with Python. You'll
have to read the docs, but i can say that Perl's
DBI has methods that will form these datastructures
for you, methods such as:
- selectall_arrayref
- selectall_hashref
- fetchrow_arrayref
- fetchrow_hashref
and more. I usually only use
selectall_arrayref,
but sometimes i need the lookup power of hashes. You have
to ensure that all of your data has unique id's in order
for hashes to work safely (without clobbering existing
keys). Recently i decided to roll my own:
# takes prepared statement handle and returns LoH
sub db2tmpl {
my ($sth) = @_;
$sth->execute;
my $fields = $sth->{NAME};
my $rows = $sth->fetchall_arrayref;
my @loh;
for my $row (@$rows) {
my %hash;
@hash{@$fields} = @$row;
push @loh, {%hash};
}
return \@loh;
}
This is very useful with
HTML::Template, though
i can't help but feel i am re-inventing a wheel. At any
rate, i wish you the best of luck. :)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
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.