Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: multidimensional array's

by Cristoforo (Deacon)
on Dec 27, 2012 at 21:26 UTC ( #1010578=note: print w/ replies, xml ) Need Help??


in reply to multidimensional array's

If you are going to address columns by name, (author, title, language, format, etc), you might want to get your database results as an array of hashes. DBI recipes is a good start here.

The little program I wrote returns all rows as an array of hashes. The text after the __END__ token shows a select * from the command line, (to show the contents of the id_minutes table).

The text after the sqlite result is the Data::Dumper output of the array of hashes.

#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; # t33.pl my $dbh = DBI->connect("dbi:SQLite:dbname=junk.lite","","", {PrintError => 1}) or die "Can't connect"; my $query = q{SELECT * FROM id_minutes}; my $aref = $dbh->selectall_arrayref($query, { Slice => {} }); $dbh->disconnect or die $dbh->errstr; print Dumper $aref; __END__ C:\Old_Data\perlp>sqlite3 junk.lite SQLite version 3.7.3 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .mode column sqlite> .width 10 10 10 sqlite> .head on sqlite> select * from id_minutes; id month minutes ---------- ---------- ---------- maclaw796 Oct 6 hturner Oct 53 nnt Oct 3 sqlite> C:\Old_Data\perlp>perl t33.pl $VAR1 = [ { 'month' => 'Oct', 'minutes' => 6, 'id' => 'maclaw796' }, { 'month' => 'Oct', 'minutes' => 53, 'id' => 'hturner' }, { 'month' => 'Oct', 'minutes' => 3, 'id' => 'nnt' } ]; C:\Old_Data\perlp>


Comment on Re: multidimensional array's
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-11-28 01:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (191 votes), past polls