Gossamer Forum
Home : General : Perl Programming :

ArrayRef to Hash

Quote Reply
ArrayRef to Hash
Hi,

I need (well don't need but would like) to use FETCHALL_ARRAYREF to select rows from mysql but I need the end result to be a hash because I need key names rather than numbers for my template parser, eg <%Foo%> not <%1%>. So I guess this is not the way to go.

Would anyone like to provide some code that will use FETCHROW_HASHREF but return the data as a hash :)

The query is.... SELECT * FROM Table ORDER BY Foo ASC LIMIT 0,25

Is it possible to use dereferencing?

If I had:

ColA, ColB, ColC

Row1, Row2, Row3

....I'm guessing the hashref would look like:

$hash = {
Row1 => { ColA, ColB, ColC },
Row2 => { ColA, ColB, ColC },
Row3 => { ColA, ColB, ColC },
};

Is that close?...that makes me think I can't use a normal hash for this.

Thanks!

Last edited by:

PaulW: Nov 20, 2001, 7:39 AM
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Code:
#!/usr/bin/perl -w
use DBI;
use strict;
my $dbh = DBI->connect("DBI:mysql:contacts",undef,undef)
or die "Unable to connect to contacts Database: $dbh->errstr\n";
$dbh->{RaiseError} = 1;
my $sth = $dbh->prepare("SELECT first_name, last_name
FROM contact");
$sth->execute or
die "Unable to execute query: $dbh->errstr\n";
my $table = $sth->fetchall_arrayref;
my($i, $j);
for $i ( 0 .. $#{$table} ) {
for $j ( 0 .. $#{$table->[$i]} ) {
print "$table->[$i][$j]\t";
}
print "\n";
}
$sth->finish;
$dbh->disconnect;
exit;

Does that help you in any way? Surely the way to do this is to use fetchrow_hashref ?

Code:
#!/usr/bin/perl -w
use DBI;
use strict;
my $dbh = DBI->connect("DBI:mysql:contacts",undef,undef)
or die "Unable to connect to contacts Database: $dbh->errstr\n";
$dbh->{RaiseError} = 1;
my $sth = $dbh->prepare("SELECT first_name, last_name
FROM contact");
$sth->execute or
die "Unable to execute query: $dbh->errstr\n";
my $row_hash;
while($row_hash = $sth->fetchrow_hashref) {
print "$row_hash->{first_name} $row_hash->{last_name}\n";
}
$sth->finish;
$dbh->disconnect;
exit;

Cheers

- wil
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Something like:
Code:
while (my ($results) = $sth->fetchrow_hashref) {
$data{$results->{$_}} = $results->{$_};
}

?
Quote Reply
Re: [Wil] ArrayRef to Hash In reply to
Sorry wil thats not what I need (I see you've been looking at the docs at last :) )

I know I can use the while loop to print the values like $hashref->{Column} but I won't always be querying the same columns.

Last edited by:

PaulW: Nov 20, 2001, 7:49 AM
Post deleted by PaulW In reply to

Last edited by:

PaulW: Nov 20, 2001, 8:01 AM
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Yep. :-) Good reference, actually ...

http://mysql.turbolift.com/mysql/DBD_3.21.X.php3

- wil
Quote Reply
Re: [Wil] ArrayRef to Hash In reply to
Hope this is clearer:

Basically I have some columns...eg....

ID, Name, URL

Now, my template parser accepts a hash eg...

<%URL%> in a template would be substituted with $hash{URL}

....however using FETCHALL_ARRAYREF obviously requires $array[0][0] (for example) so I need some way of converting it to a hash for my parser.

I suppose I could add extra code to the parser to check what is being passed to the parse routine but I just thought there may be an easier way than using:

if (ref $var eq 'ARRAY') {

Bla Bla

But even that doesn't solve the problem as I need keys with names not numbers

Last edited by:

PaulW: Nov 20, 2001, 8:24 AM
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Well isn't that what FETCHALL_HASHREF is for ??

- wil
Quote Reply
Re: [Wil] ArrayRef to Hash In reply to
FETCHALL_HASHREF doesn't exist Tongue
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Sorry, I meant fetchrow_hashref ?

- wil
Quote Reply
Re: [Wil] ArrayRef to Hash In reply to
Yeah but thats what Im asking. I need to return a hash, so I need the hashref to be converted to a hash.
Quote Reply
Re: [PaulW] ArrayRef to Hash In reply to
Then I don't have a clue what I'm on about. Good luck :-) ...

- wil
Quote Reply
Re: [Wil] ArrayRef to Hash In reply to
Nevermind I solved it with a compromise :)

Code:
while (my ($cols) = $sth->fetchrow_hashref) {
$data->{$cols->{ID}} = \%{$cols};
}

Then I can access the different values with $data->{$cols->{ID}}->{Column};

Thanks anyway.

Last edited by:

PaulW: Nov 20, 2001, 9:41 AM