MySql - push a hash into an array

Discussion in 'Perl Misc' started by John, Oct 2, 2006.

  1. John

    John Guest

    Hi

    I am reading a row of hashes from a MySQL table.
    I need to add that row to an array to create an 'array of hashes'
    I should then be able to access, say, $xyz[34]{'steel'}.

    The following is part of the code:

    my @xyz=({}); # array of hashes

    $sql="SELECT coal,iron,steel,china,wool,cloth,fish,livestock FROM industry";
    $sth=$dbh->prepare($sql);
    $sth->execute or die "Unable to execute query: $dbh->errstr\n";

    my $row;
    while ($row = $sth->fetchrow_hashref) {push (@xyz,$row)}

    $sth->finish();
    $dbh- >disconnect();

    print $xyz[6]{'cloth'};

    The print statement is empty. Any ideas?

    Regards
    John
    John, Oct 2, 2006
    #1
    1. Advertising

  2. John

    Paul Lalli Guest

    John wrote:
    > I am reading a row of hashes from a MySQL table.
    > I need to add that row to an array to create an 'array of hashes'
    > I should then be able to access, say, $xyz[34]{'steel'}.
    >
    > The following is part of the code:
    >
    > my @xyz=({}); # array of hashes


    There is no need for that initialization. Indeed, it will hurt you in
    the long run. You are specifically adding one completely empty hash
    reference to your array. Therefore, when all is said and done, your
    array will have one more hash than the number of rows in your data set.
    Change to:
    my @xyz;

    > $sql="SELECT coal,iron,steel,china,wool,cloth,fish,livestock FROM industry";
    > $sth=$dbh->prepare($sql);
    > $sth->execute or die "Unable to execute query: $dbh->errstr\n";
    >
    > my $row;


    > while ($row = $sth->fetchrow_hashref) {push (@xyz,$row)}
    >
    > $sth->finish();
    > $dbh- >disconnect();
    >
    > print $xyz[6]{'cloth'};
    >
    > The print statement is empty. Any ideas?


    Well the first and most obvious is: Are you sure that the statement
    handler returned at least 7 rows? The second and second-most obvious
    is: Are you sure that the 7th row's value for the 'cloth' column was
    neither NULL nor the empty string?

    What do you get for these two lines?

    use Data::Dumper;
    print Dumper(\@xyz);

    Finally, are you aware that DBI has a means by which to do this
    already?
    my @xyz = @{ $sth->fetchall_arrayref( { } ) };

    Paul Lalli
    Paul Lalli, Oct 2, 2006
    #2
    1. Advertising

  3. John

    John Guest

    Thank you, gentlemen.

    I can now see my mistake.

    It's proably better to use the fetchall construct.

    Thanks again and regards
    John
    John, Oct 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. XyZaa
    Replies:
    0
    Views:
    567
    XyZaa
    Jul 19, 2007
  2. rp
    Replies:
    1
    Views:
    520
    red floyd
    Nov 10, 2011
  3. samppi
    Replies:
    27
    Views:
    480
    David A. Black
    Dec 5, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    617
    David A. Black
    Jul 2, 2008
  5. Replies:
    14
    Views:
    107
    Thomas 'PointedEars' Lahn
    May 21, 2014
Loading...

Share This Page