To expand somewhat on Corion's reply, let's say you have a mysql database called 'new', and a table 'employee', which looks like this:
mysql> select * from employee; +----+---------+--------+------------+ | id | name | salary | hiredate | +----+---------+--------+------------+ | 1 | Fran | 100000 | 2006-05-01 | | 2 | Barry | 90000 | 2006-04-01 | | 3 | Anna | 80000 | 2006-01-01 | | 4 | Cynthia | 70000 | 2006-02-01 | | 5 | Enrico | 60000 | 2006-03-01 | | 6 | Derek | 50000 | 2006-06-01 | +----+---------+--------+------------+ 6 rows in set (0.00 sec)
The following program:
#!/usr/bin/perl -wT # # Written to illustrate various mechanisms of data retrieval from # a mysql database 'new'. # # 060402 liverpole -- created. # ############## ### Strict ### ############## use strict; use warnings; ################# ### Libraries ### ################# use Data::Dumper; use DBI; #################### ### Main program ### #################### my $dbh = connect_to_dbase('DBI:mysql:database=new', 'root', 'rootpass +'); my $sql = "SELECT * FROM employee"; my $p1 = sql_hash($dbh, $sql); printf "Results as hash => '%s'\n", Dumper($p1); my $p2 = sql_array($dbh, $sql); printf "Results as array => '%s'\n", Dumper($p2); ################### ### Subroutines ### ################### sub connect_to_dbase { my ($dsn, $user, $pass) = @_; my $dbh = DBI->connect($dsn, $user, $pass, { 'AutoCommit' => 1 }); defined $dbh or die "Cannot connect to database '$dsn'\n"; return $dbh; } sub sql_hash { my ($dbh,$sql) = @_; my $sth = $dbh->prepare($sql); $sth->execute() or die "Failed to execute SQL '$sql'\n"; my @data; while (my $p = $sth->fetchrow_hashref) { push @data, $p; } return \@data; } sub sql_array { my ($dbh,$sql) = @_; my $sth = $dbh->prepare($sql); $sth->execute() or die "Failed to execute SQL '$sql'\n"; my @data; while (my $p = $sth->fetchrow_arrayref) { push @data, [ @$p ]; } return \@data; }
illustrates the differences when you fetch the data using a hashref or an arrayref:
Results as hash => '$VAR1 = [ { 'name' => 'Fran', 'hiredate' => '2006-05-01', 'id' => '1', 'salary' => '100000' }, { 'name' => 'Barry', 'hiredate' => '2006-04-01', 'id' => '2', 'salary' => '90000' }, { 'name' => 'Anna', 'hiredate' => '2006-01-01', 'id' => '3', 'salary' => '80000' }, { 'name' => 'Cynthia', 'hiredate' => '2006-02-01', 'id' => '4', 'salary' => '70000' }, { 'name' => 'Enrico', 'hiredate' => '2006-03-01', 'id' => '5', 'salary' => '60000' }, { 'name' => 'Derek', 'hiredate' => '2006-06-01', 'id' => '6', 'salary' => '50000' } ]; ' Results as array => '$VAR1 = [ [ '1', 'Fran', '100000', '2006-05-01' ], [ '2', 'Barry', '90000', '2006-04-01' ], [ '3', 'Anna', '80000', '2006-01-01' ], [ '4', 'Cynthia', '70000', '2006-02-01' ], [ '5', 'Enrico', '60000', '2006-03-01' ], [ '6', 'Derek', '50000', '2006-06-01' ] ]; '
But if you want the order stored in a particular way, for example, sorted by employee name, you will either have to perform the ordering in the SQL statement (eg. "SELECT * FROM employee ORDER BY name"), or you will have to perform the sort after retrieving the data from the database.  For example, to sort the hash references by name:
sub sql_hash { my ($dbh,$sql) = @_; my $sth = $dbh->prepare($sql); $sth->execute() or die "Failed to execute SQL '$sql'\n"; my @data; while (my $p = $sth->fetchrow_hashref) { push @data, $p; } my @sorted = sort { $a->{'name'} cmp $b->{'name'} } @data; return \@sorted; }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Oder of HASH ARRAYS by liverpole
in thread Oder of HASH ARRAYS by dumpexec

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.