RE: iterating through hash of hash references
Andrew Gaffney [EMAIL PROTECTED] wrtoe: :foreach (keys %$tabledesc) { : foreach (keys %$tabledesc-{$_}) { Shouldn't that be: foreach ( keys %{ $tabledesc-{$_} } ) { :print $_ = $tabledesc-{$_}, ; : } : print \n; :} BTW, it's confusing to use nested loops which both use the default value ($_). You would probably be better off with named values or just not using two loops. use Data::Dumper 'Dumper'; print Dumper $tabledesc; HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: iterating through hash of hash references
Charles K. Clarkson wrote: Andrew Gaffney [EMAIL PROTECTED] wrtoe: :foreach (keys %$tabledesc) { : foreach (keys %$tabledesc-{$_}) { Shouldn't that be: foreach ( keys %{ $tabledesc-{$_} } ) { :print $_ = $tabledesc-{$_}, ; : } : print \n; :} BTW, it's confusing to use nested loops which both use the default value ($_). You would probably be better off with named values or just not using two loops. use Data::Dumper 'Dumper'; print Dumper $tabledesc; I changed it to: foreach my $field (keys %$tabledesc) { print ${field} - ; foreach (keys %{ $tabledesc-{$field} }) { print $_ = $tabledesc-{$field}-{$_}, ; } print \n; } which makes it a tad less confusing (and also makes it work). Now I've got another issue. How can I dynamically assign new keys to a hash without getting warnings when using 'use warnings'? I get the warning about an undefined value on the line with the '-'. $tabledesc = {}; $sth = $dbh-prepare(DESCRIBE $_); $sth-execute; while($ref = $sth-fetchrow_hashref) { - $tabledesc-{$ref-{Field}} = {type= $ref-{Type}, null= $ref-{Null}, key = $ref-{Key}, default = $ref-{Default}, extra = $ref-{Extra} }; } -- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: iterating through hash of hash references
Andrew Gaffney [EMAIL PROTECTED] wrote: : Now I've got another issue. : How can I dynamically assign new keys to a hash without : getting warnings when using 'use warnings'? I get the warning : about an undefined value on the line with the '-'. You can either temporarily turn those warnings off (not a good idea) or you can make certain undefined values are not used. The big question would be: Why is '$sth-fetchrow_hashref' returning at least one undefined value? :$tabledesc = {}; :$sth = $dbh-prepare(DESCRIBE $_); :$sth-execute; :while($ref = $sth-fetchrow_hashref) { Are you sure your DB returns Field, Type, Null, Key, Default, and Extra on every field? Perhaps a more general approach would work: my $field = delete $ref-{Field}; $tabledesc-{ $field } = $ref; : - $tabledesc-{$ref-{Field}} = {type= $ref-{Type}, : null= $ref-{Null}, : key = $ref-{Key}, : default = $ref-{Default}, : extra = $ref-{Extra} }; What are the double quotes for? You're forcing stringification (perlfaq4). That's usually a bad idea. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: iterating through hash of hash references
%$tabledesc-{$_} is confused ... Try writing it like : %{ $tabledesc-{$_} } HTH, José. -Original Message- From: Andrew Gaffney [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 03, 2004 2:50 PM To: beginners Subject: iterating through hash of hash references I have a script which creates a hash where all the values are hash references. I'm trying to iterate through the original hash and down into the anonymous hashes to make sure my code is doing what I think it's doing. My problem comes in at those nested foreach loops before the DB disconnect. I get the error: Using a hash as a reference is deprecated at ./modtables.pl line 24. Type of arg 1 to keys must be hash (not hash element) at ./modtables.pl line 24, near }) code #!/usr/bin/perl use Skyline; # Custom module use strict; use warnings; my @tables; my $dbh = init_db(); # Custom function to connect to MySQL DB my ($sth, $ref, $tabledesc); get_table_list(); foreach (@tables) { $tabledesc = {}; $sth = $dbh-prepare(DESCRIBE $_); $sth-execute; while($ref = $sth-fetchrow_hashref) { $tabledesc-{$ref-{Field}} = {type= $ref-{Type}, null= $ref-{Null}, key = $ref-{Key}, default = $ref-{Default}, extra = $ref-{Extra} }; } foreach (keys %$tabledesc) { foreach (keys %$tabledesc-{$_}) { print $_ = $tabledesc-{$_}, ; } print \n; } } $dbh-disconnect(); sub get_table_list { $sth = $dbh-prepare(SHOW TABLES); $sth-execute; while($ref = $sth-fetchrow_hashref) { push @tables, $ref-{'Tables_in_skyline'} if($ref-{'Tables_in_skyline'} =~ /^m\d{6}$/); } } /code -- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: iterating through hash of hash references
Charles K. Clarkson wrote: Andrew Gaffney [EMAIL PROTECTED] wrote: : Now I've got another issue. : How can I dynamically assign new keys to a hash without : getting warnings when using 'use warnings'? I get the warning : about an undefined value on the line with the '-'. You can either temporarily turn those warnings off (not a good idea) or you can make certain undefined values are not used. The big question would be: Why is '$sth-fetchrow_hashref' returning at least one undefined value? It looks like not all of the fields have something in them. Perl is taking blanks for undefined for some reason. :$tabledesc = {}; :$sth = $dbh-prepare(DESCRIBE $_); :$sth-execute; :while($ref = $sth-fetchrow_hashref) { Are you sure your DB returns Field, Type, Null, Key, Default, and Extra on every field? Perhaps a more general approach would work: my $field = delete $ref-{Field}; $tabledesc-{ $field } = $ref; : - $tabledesc-{$ref-{Field}} = {type= $ref-{Type}, : null= $ref-{Null}, : key = $ref-{Key}, : default = $ref-{Default}, : extra = $ref-{Extra} }; What are the double quotes for? You're forcing stringification (perlfaq4). That's usually a bad idea. I was trying to get rid of another warning: Use of uninitialized value in concatenation (.) or string at ./modtables.pl line 26. Which is the line where it prints out the values in the hash. It gives me that when the field is blank, the same as it give me the other warning with the quotes. -- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: iterating through hash of hash references
Hello, It looks like not all of the fields have something in them. Perl is taking blanks for undefined for some reason. An empty field in a database is not a blank, as you say, but a NULL. Meaning it's not just blank, but effectively inexistant - there is no value, not even the empty string, which would be a blank. DBI translates a NULL value as undef, which means pretty much the same thing in Perl. That's why you're seeing some undef warnings, because you're getting some NULL values. So you either have to check for them, or replace all NULL values in your DB with (empty string, which is empty but defined) - but you may not want to do that for other reasons. Of course, if you can't make that decision because you don't administer the database, then you'll have to check for undef each time (unless your table specifies a field as NOT NULL, meaning that the field cannot be NULL and the database engine will enforce that rule). Hope that helps, J-S -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: iterating through hash of hash references
Andrew Gaffney [EMAIL PROTECTED] wrote: : : It looks like not all of the fields have something in : them. Perl is taking blanks for undefined for some : reason. : : :$tabledesc = {}; : :$sth = $dbh-prepare(DESCRIBE $_); : :$sth-execute; : :while($ref = $sth-fetchrow_hashref) { Try adding this: foreach my $value ( values %$ref ) { $value = '' unless defined $value; } HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response