Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: multidimensional array's

by NetWallah (Abbot)
on Dec 27, 2012 at 23:40 UTC ( #1010598=note: print w/ replies, xml ) Need Help??


in reply to multidimensional array's

fetchall_arrayref will return what you say you want to accomplish - a single call that returns a multi-dimensional array. You will not need the "while" loop, unless you want to iterate the array, in which case, a "for" loop is more appropriate.

Here is sample code:

my @allrows = @{ $sth->fetchrow_arrayref() }; # Expand ref, to simplif +y access my $first_author = $allrows[0][0]; my $second_author = $allrows[1][0]; my $first_title = $allrows[0][1];
Also, access to a scalar element of an array is correctly written as
$language = $row[2]; # Correct $language = @$row[2]; # wrong (at least in perl ver 5)

             "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius


Comment on Re: multidimensional array's
Select or Download Code
Replies are listed 'Best First'.
Re^2: multidimensional array's
by space_monk (Chaplain) on Dec 28, 2012 at 10:43 UTC
    A little known "feature" of fetchall_arrayref is that it will return an array of hashes if you supply it a hash, and if that hash is empty, it will return all the fields.
    my @allrows = @{ $sth->fetchrow_arrayref({}) }; # Expand ref, to simpl +ify access

    YMMV, but I always find it easier to look at $row{language} than $row[2]. I'm sure there's a mild performance hit, but as long as it's not incredibly slow I'd opt for clarity.

    A Monk aims to give answers to those who have none, and to learn from those who know more.
      Ah - learned something today - thank you.

      I avoid "fetchall_*", because I mostly process a row-at-a-time , and use the clearer syntax of fetchrow_hashref.

      The OP's questions were based on managing arrays, and I did not want to bring hashes and confuse that discussion.

      Thanks, space_monk.

                   "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

        @All: Thnx a lot for your helpful answers.

        With some additional reading and fiddling around with the syntax I have figured this out now.

        my @allrows =(); while ($row = $sth->fetchrow_arrayref() ) { push @allrows,[@$row]; }; my $titlesfound = @allrows; # Contains the number of results #Show that it works: $loopcount = 0; foreach (@allrows) { my $title1 = ($allrows[$loopcount][1]); print "$title1\n"; $loopcount++; };

        /emgi

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-07-30 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls