Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can i iterate my hash db results if I do not know the field names

Status
Not open for further replies.

justride

Programmer
Jan 9, 2004
251
US
I am trying to make a dynamic fucntion to display results from a DBI call.

I know the # of rows, but I do not know the number of fields or what the field names are
so I need to somehow iterate through my record set.

Code:
while( my $rows = $record_set->fetchrow_hashref() ) {
print  $rows->{'unknowns'}\n"; 
}

Any advice on how ti display all the data?

Thanks
 
The function keys will give you a list containing all the keys of the hash.

Code:
rows = $record_set->fetchrow_hashref();
$field_names = keys %{$rows};

: Daniel :

-
 
hmmm, you you explain that a bit more, when I print $field_names i get 1 bunch of 0's and 1's.


Please excuse my ignorance but I dont think Im understanding this hash ref properly. The scalar $rows, during each iteration is = to a regrence to a hash right? I need to be able to iterate through that hash every time, each hash dynamically.

If I can get the keys like you suggested above, that is beneficial to, there for I can display the keys first.

Thanks
 
Make it @field_names instead of $field_names. You need to assign keys to a list.
 
Code:
foreach my $key (keys %$rows) {
print "$key => $$rows{$key}\n";
}

This seems to work as an alternative, im not sure what the $$ does, I got this off another site.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top