Printing A Hash of Hashes
From a previous post I needed to identify a particular data structure. It is a hash of hashes. Now, I need to print the hash out. I'm using the code from the "Programing Perl" 3rd edition from O'Reilly on how to print out the hash and of course I'm having a problem. This is from Chapter 9, page 281 slighly changed to reflect my values: for $table ( keys %tablename) { print "Table Name: $table \n"; for $items ( keys %{ $tablename{$table} } ) { print "\t$items=$tablename{$table}{$items}\n "; } print "\n"; Here are the results: Table Name: TURBINE_PERMISSION con_name=ARRAY(0x216de8) index_name=TURBINE_PERMISSION_PK con_type=ARRAY(0x216c8c) columns=PERMISSION_ID created_by=ARRAY(0x211a50) type=NORMAL rem_con_name=ARRAY(0x211b04) tablespace=CTNG This is the code that created the hash of hashes. It was done in 2 parts: First part (initilizing): $tablename{"$table"} = { "index_name"=> $index_name, "columns" => @column_name, "type" => $index_type, "tablespace"=> $tablespace_name }; Adding to the hash: $tablename{$table} -> {con_name} = [$constraint_name]; $tablename{$table} -> {con_type} = [$type]; $tablename{$table} -> {rem_con_name} = [$r_constraint_name]; $tablename{$table} -> {created_by} = [$generated]; The above is from the help (thank you) from a previous post. Why am I getting the value in the first part, and the memory location for the second part? In other words, the key/values print out as expected from the initializing loop, but not from the "adding" part? -- Ken Hammer University Of Michigan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Printing A Hash of Hashes
> This is from Chapter 9, page 281 slighly >changed to reflect my values: > >for $table ( keys %tablename) { >print "Table Name: $table \n"; > >for $items ( keys %{ $tablename{$table} } ) { >print "\t$items=$tablename{$table}{$items}\n "; > >} > >print "\n"; That looks reasonable. > Here are the results: > >Table Name: TURBINE_PERMISSION >con_name=ARRAY(0x216de8) >index_name=TURBINE_PERMISSION_PK >con_type=ARRAY(0x216c8c) >columns=PERMISSION_ID >created_by=ARRAY(0x211a50) >type=NORMAL >rem_con_name=ARRAY(0x211b04) >tablespace=CTNG It looks like some of the values in your hash are arrays. > This is the code that created the hash of hashes. >It was done in 2 parts: > > First part (initilizing): > >$tablename{"$table"} = { >"index_name"=> $index_name, >"columns" => @column_name, >"type" => $index_type, >"tablespace"=> $tablespace_name > >}; > > Adding to the hash: > >$tablename{$table} -> {con_name} = [$constraint_name]; >$tablename{$table} -> {con_type} = [$type]; >$tablename{$table} -> {rem_con_name} = [$r_constraint_name]; >$tablename{$table} -> {created_by} = [$generated]; > > The above is from the help (thank you) from a previous post. Why are you wrapping these in arrays? If you used: $tablename{$table} -> {con_name} = $constraint_name; $tablename{$table} -> {con_type} = $type; $tablename{$table} -> {rem_con_name} = $r_constraint_name; $tablename{$table} -> {created_by} = $generated; Then you would get the values in your print, instead of array indexes. > Why am I getting the value in the first part, and the >memory location for the second part? In other words, the key/values >print out as expected from the initializing loop, but not from >the "adding" part? I hope that I answered your question above. The other option is to test each value to see if it is an array, and then dereference it correctly, but I don't think that's what you are looking for. /\/\ark -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Printing A Hash of Hashes
In your adding lines you add entries line [ $entry ], which means, make an array reference (the brackets) which has $entry as the first element of the array. You then have a hash of hashes of arrays (for at least some entries). If you meant the arrays, you'll have to add a third loop to the printing code to account for that level of nesting. If you didn't intend to make arrays there, just drop those brackets. James Gray On Monday, September 30, 2002, at 02:59 PM, Ken Hammer wrote: > > From a previous post I needed to identify > a particular data structure. It is a hash of hashes. > Now, I need to print the hash out. > I'm using the code from the "Programing > Perl" 3rd edition from O'Reilly on how > to print out the hash and of course I'm > having a problem. > > This is from Chapter 9, page 281 slighly > changed to reflect my values: > > for $table ( keys %tablename) { > print "Table Name: $table \n"; > > for $items ( keys %{ $tablename{$table} } ) { > print "\t$items=$tablename{$table}{$items}\n "; > > } > > print "\n"; > > Here are the results: > > Table Name: TURBINE_PERMISSION > con_name=ARRAY(0x216de8) > index_name=TURBINE_PERMISSION_PK > con_type=ARRAY(0x216c8c) > columns=PERMISSION_ID > created_by=ARRAY(0x211a50) > type=NORMAL > rem_con_name=ARRAY(0x211b04) > tablespace=CTNG > > This is the code that created the hash of hashes. > It was done in 2 parts: > > First part (initilizing): > > $tablename{"$table"} = { > "index_name"=> $index_name, > "columns" => @column_name, > "type" => $index_type, > "tablespace"=> $tablespace_name > > }; > > Adding to the hash: > > $tablename{$table} -> {con_name} = [$constraint_name]; > $tablename{$table} -> {con_type} = [$type]; > $tablename{$table} -> {rem_con_name} = [$r_constraint_name]; > $tablename{$table} -> {created_by} = [$generated]; > > The above is from the help (thank you) from a previous post. > > Why am I getting the value in the first part, and the > memory location for the second part? In other words, the key/values > print out as expected from the initializing loop, but not from > the "adding" part? > > -- > Ken Hammer > University Of Michigan > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Printing A Hash of Hashes
Mark Anderson wrote: > > Why are you wrapping these in arrays? If you used: > $tablename{$table} -> {con_name} = $constraint_name; > $tablename{$table} -> {con_type} = $type; > $tablename{$table} -> {rem_con_name} = $r_constraint_name; > $tablename{$table} -> {created_by} = $generated; > > Then you would get the values in your print, instead of array indexes. > Bingo!!! That was it Mark! Thank you! > I hope that I answered your question above. The other option is to test > each value to see if it is an array, and then dereference it correctly, but > I don't think that's what you are looking for. > > /\/\ark -- Ken Hammer University Of Michigan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Printing a hash of hashes of arrays
Pure beginners question. I'm creating a hash of arrays like this : $ihash{$3}{$1} = [...@itab]; For now I was able to get the data using Dumper but I need to create a "pretty" report. How do I loop over this hash/hash of arrays to print it out? Thank you. -- Ian
Re: Printing a hash of hashes of arrays
On 8/27/09 Thu Aug 27, 2009 10:42 AM, "Ian" scribbled: > Pure beginners question. > > I'm creating a hash of arrays like this : > > $ihash{$3}{$1} = [...@itab]; > > For now I was able to get the data using Dumper but I need to create a > "pretty" report. > > How do I loop over this hash/hash of arrays to print it out? Something like this: for my $key1 ( sort keys %ihash ) { for my $key2 ( sort keys %{$ihash{$key1}} ) { for my $element ( @{$ihash{$key1}->{$key2}} ) { printf " %10s %10s %s\n", $key1, $key2, $element; } } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Printing a hash of hashes of arrays
> "I" == Ian writes: I> Pure beginners question. I> I'm creating a hash of arrays like this : I> $ihash{$3}{$1} = [...@itab]; I> For now I was able to get the data using Dumper but I need to create a I> "pretty" report. I> How do I loop over this hash/hash of arrays to print it out? you got a direct answer from someone else but you should learn the generic way to do this. read perldoc perldsc and perllol for more on how to build and scan perl data trees. the concepts are very basic and once you know them, you can scan any tree you have. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Printing a hash of hashes of arrays
Thank you David, Jim, Uri.
RE: Printing a hash of hashes of arrays
> -Original Message- > From: Ian [mailto:pcs...@gmail.com] > Sent: Thursday, August 27, 2009 11:43 > To: beginners@perl.org > Subject: Printing a hash of hashes of arrays > > Pure beginners question. > > I'm creating a hash of arrays like this : > > $ihash{$3}{$1} = [...@itab]; > > For now I was able to get the data using Dumper but I need to create a > "pretty" report. > > How do I loop over this hash/hash of arrays to print it out? You did not give any real details as to what the data would look like other than hash and array. What is in the array? Numbers? Alpha? AlphaNumic? Here is a shot which is very simplistic: #!/usr/bin/perl use strict; use warnings; my %ihash = (); $ihash{a}{a} = [1,2,3,4,5]; $ihash{a}{b} = [2,3,4,5,6]; $ihash{b}{a} = [3,4,5,6,7]; my $wrkkeya; my $wrkkeyb; for $wrkkeya (sort keys %ihash) { for $wrkkeyb ( sort keys %{$ihash{$wrkkeya}} ) { printf "%-3s%-3s ", $wrkkeya, $wrkkeyb; for (@{$ihash{$wrkkeya}{$wrkkeyb}} ) { printf "%3d", $_; } print "\n"; } } Output: a a 1 2 3 4 5 a b 2 3 4 5 6 b a 3 4 5 6 7 A start... If you have any questions and/or problems, please let me know. Thanks. Wags ;) David R. Wagner Senior Programmer Analyst FedEx Freight Systems 1.719.484.2097 Tel 1.719.484.2419 Fax 1.408.623.5963 Cell http://fedex.com/us > > > Thank you. > > -- > Ian > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/