Need to print hash of hash in table format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to print hash of hash in table format
# 1  
Old 11-18-2016
Error Need to print hash of hash in table format

Hi, I have a hash of hash where it has
name, activities and count

i have data like this -
Code:
$result->{$name}->{$activities} = $value;

content of that are -
name - robert tom cat peter
activities - running, eating, sleeping , drinking, work

i need to print output as below
Code:
             Robert  tom    cat    peter
running      10      8        3        3
eating        20      5        3        2
sleeping      9       9        8        10
drinking      0       1        2        0
work          10     10       9       10

# 2  
Old 11-18-2016
Code:
$ 
$ cat -n print_hoh.pl
     1	#!/usr/bin/perl -w
     2	use strict;
     3	my ($result, $name, $activities, $value);
     4	
     5	# Subroutine section
     6	sub build_hoh {
     7	    ($name, $activities, $value) = @_;
     8	    $result->{$name}->{$activities} = $value;
     9	}
    10	
    11	sub print_hoh {
    12	    my @values = ();
    13	    my @names = sort keys %{$result};
    14	    my @activities = sort keys %{$result->{$names[0]}};
    15	    my $hdr_fmt = "%10s|%-6s|%-6s|%-6s|%-6s|\n";
    16	    my $row_fmt = "%-10s|%6d|%6d|%6d|%6d|\n";
    17	    printf($hdr_fmt, "", @names);
    18	    foreach my $act (@activities) {
    19	        foreach my $name (@names) {
    20	            push(@values, $result->{$name}->{$act});
    21	        }
    22	        printf($row_fmt, $act, @values);
    23	        @values = ();
    24	    }
    25	}
    26	
    27	# Main section
    28	build_hoh("robert", "running", 10);
    29	build_hoh("robert", "eating", 20);
    30	build_hoh("robert", "sleeping", 9);
    31	build_hoh("robert", "drinking", 0);
    32	build_hoh("robert", "work", 10);
    33	
    34	build_hoh("tom", "running", 8);
    35	build_hoh("tom", "eating", 5);
    36	build_hoh("tom", "sleeping", 9);
    37	build_hoh("tom", "drinking", 1);
    38	build_hoh("tom", "work", 10);
    39	
    40	build_hoh("cat", "running", 3);
    41	build_hoh("cat", "eating", 3);
    42	build_hoh("cat", "sleeping", 8);
    43	build_hoh("cat", "drinking", 2);
    44	build_hoh("cat", "work", 9);
    45	
    46	build_hoh("peter", "running", 3);
    47	build_hoh("peter", "eating", 2);
    48	build_hoh("peter", "sleeping", 10);
    49	build_hoh("peter", "drinking", 0);
    50	build_hoh("peter", "work", 10);
    51	
    52	print_hoh;
    53	
$ 
$ perl print_hoh.pl
          |cat   |peter |robert|tom   |
drinking  |     2|     0|     0|     1|
eating    |     3|     2|    20|     5|
running   |     3|     3|    10|     8|
sleeping  |     8|    10|     9|     9|
work      |     9|    10|    10|    10|
$ 
$

Note that the "keys" function returns the keys of a hash in an apparently random order.
I sorted names as well as activities, so the values in my output are ordered horizontally by name and vertically by activities.
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 11-18-2016
HI,

Thanks for the code, but the header and row are hard coded, in my case it an variable, some times i will get 4 name and some time 10 names. how to accommodate that?

---------- Post updated at 07:38 PM ---------- Previous update was at 07:35 PM ----------

Quote:
Originally Posted by durden_tyler
Code:
$ 
$ cat -n print_hoh.pl
     1	#!/usr/bin/perl -w
     2	use strict;
     3	my ($result, $name, $activities, $value);
     4	
     5	# Subroutine section
     6	sub build_hoh {
     7	    ($name, $activities, $value) = @_;
     8	    $result->{$name}->{$activities} = $value;
     9	}
    10	
    11	sub print_hoh {
    12	    my @values = ();
    13	    my @names = sort keys %{$result};
    14	    my @activities = sort keys %{$result->{$names[0]}};
    15	    my $hdr_fmt = "%10s|%-6s|%-6s|%-6s|%-6s|\n";
    16	    my $row_fmt = "%-10s|%6d|%6d|%6d|%6d|\n";
    17	    printf($hdr_fmt, "", @names);
    18	    foreach my $act (@activities) {
    19	        foreach my $name (@names) {
    20	            push(@values, $result->{$name}->{$act});
    21	        }
    22	        printf($row_fmt, $act, @values);
    23	        @values = ();
    24	    }
    25	}
    26	
    27	# Main section
    28	build_hoh("robert", "running", 10);
    29	build_hoh("robert", "eating", 20);
    30	build_hoh("robert", "sleeping", 9);
    31	build_hoh("robert", "drinking", 0);
    32	build_hoh("robert", "work", 10);
    33	
    34	build_hoh("tom", "running", 8);
    35	build_hoh("tom", "eating", 5);
    36	build_hoh("tom", "sleeping", 9);
    37	build_hoh("tom", "drinking", 1);
    38	build_hoh("tom", "work", 10);
    39	
    40	build_hoh("cat", "running", 3);
    41	build_hoh("cat", "eating", 3);
    42	build_hoh("cat", "sleeping", 8);
    43	build_hoh("cat", "drinking", 2);
    44	build_hoh("cat", "work", 9);
    45	
    46	build_hoh("peter", "running", 3);
    47	build_hoh("peter", "eating", 2);
    48	build_hoh("peter", "sleeping", 10);
    49	build_hoh("peter", "drinking", 0);
    50	build_hoh("peter", "work", 10);
    51	
    52	print_hoh;
    53	
$ 
$ perl print_hoh.pl
          |cat   |peter |robert|tom   |
drinking  |     2|     0|     0|     1|
eating    |     3|     2|    20|     5|
running   |     3|     3|    10|     8|
sleeping  |     8|    10|     9|     9|
work      |     9|    10|    10|    10|
$ 
$

Note that the "keys" function returns the keys of a hash in an apparently random order.
I sorted names as well as activities, so the values in my output are ordered horizontally by name and vertically by activities.
Hi Tyler,

Thanks for the code, but the header and row are hard coded, in my case it an variable, some times i will get 4 name and some time 10 names. how to accommodate that?
# 4  
Old 11-18-2016
Quote:
Originally Posted by asak
... but the header and row are hard coded, in my case it an variable, some times i will get 4 name and some time 10 names. how to accommodate that?
....
If the names and activities are variable, we can have a name that has a completely new activity that is not associated with any other name.
So in that case, the "activities" array should be built by looping through all names and their corresponding activities in the entire hash-of-hash data structure. That is, the following line in my earlier post:

Code:
    14	    my @activities = sort keys %{$result->{$names[0]}};

should change.

I have also changed the following two lines.

Code:
    15	    my $hdr_fmt = "%10s|%-6s|%-6s|%-6s|%-6s|\n";
    16	    my $row_fmt = "%-10s|%6d|%6d|%6d|%6d|\n";

The header and row format strings are now put inside a function that builds these strings dynamically, depending on the "names" array.

The modified code is below. The modifications are in red color.

Code:
$ 
$ cat -n print_hoh.pl 
     1	#!/usr/bin/perl -w
     2	use strict;
     3	my ($result, $name, $activities, $value, $hdr_fmt, $row_fmt);
     4	
     5	# Subroutine section
     6	sub build_hoh {
     7	    ($name, $activities, $value) = @_;
     8	    $result->{$name}->{$activities} = $value;
     9	}
    10	
    11	sub build_fmt_strings {
    12	    my $aref = shift;
    13	    $hdr_fmt = "%10s|";
    14	    $row_fmt = "%-10s|";
    15	    foreach my $i (@{$aref}) {
    16	        $hdr_fmt .= "%-6s|";
    17	        $row_fmt .= "%6s|";
    18	    }
    19	    $hdr_fmt .= "\n";
    20	    $row_fmt .= "\n";
    21	}
    22	
    23	sub print_hoh {
    24	    my @values = ();
    25	    my @names = sort keys %{$result};
    26	    # Since the activities could vary across names, loop through
    27	    # all activities, collect them in all_activities hash and sort
    28	    my %all_activities;
    29	    foreach my $i (keys %{$result}) {
    30	        foreach my $j (keys %{$result->{$i}}) {
    31	            $all_activities{$j}++;
    32	        }
    33	    }
    34	    my @activities = sort keys %all_activities;
    35	    # Now build the format strings and start printing
    36	    build_fmt_strings(\@names);
    37	    printf($hdr_fmt, "", @names);
    38	    foreach my $act (@activities) {
    39	        foreach my $name (@names) {
    40	            push(@values, $result->{$name}->{$act} // "");
    41	        }
    42	        printf($row_fmt, $act, @values);
    43	        @values = ();
    44	    }
    45	}
    46	
    47	# Main section
    48	build_hoh("robert", "running", 10);
    49	build_hoh("robert", "eating", 20);
    50	build_hoh("robert", "sleeping", 9);
    51	build_hoh("robert", "drinking", 0);
    52	build_hoh("robert", "work", 10);
    53	
    54	build_hoh("tom", "running", 8);
    55	build_hoh("tom", "eating", 5);
    56	build_hoh("tom", "sleeping", 9);
    57	build_hoh("tom", "drinking", 1);
    58	build_hoh("tom", "work", 10);
    59	
    60	build_hoh("cat", "running", 3);
    61	build_hoh("cat", "eating", 3);
    62	#build_hoh("cat", "sleeping", 8);
    63	build_hoh("cat", "drinking", 2);
    64	build_hoh("cat", "work", 9);
    65	
    66	build_hoh("peter", "running", 3);
    67	build_hoh("peter", "eating", 2);
    68	build_hoh("peter", "sleeping", 10);
    69	build_hoh("peter", "drinking", 0);
    70	build_hoh("peter", "work", 10);
    71	
    72	build_hoh("john", "hiking", 99);
    73	build_hoh("john", "work", 88);
    74	
    75	print_hoh;
    76	
$ 
$ 
$ perl print_hoh.pl
          |cat   |john  |peter |robert|tom   |
drinking  |     2|      |     0|     0|     1|
eating    |     3|      |     2|    20|     5|
hiking    |      |    99|      |      |      |
running   |     3|      |     3|    10|     8|
sleeping  |      |      |    10|     9|     9|
work      |     9|    88|    10|    10|    10|
$ 
$

---------- Post updated at 10:41 PM ---------- Previous update was at 10:13 PM ----------

In my next program, I have moved the variable data in a separate file altogether. It's the data file in csv format.
The data file name is passed to the Perl program, which then reads the data from the data file and prints the table dynamically.

Code:
$ 
$ # The data file
$ cat hoh_data.txt
robert,running,10
robert,eating,20
robert,sleeping,9
robert,drinking,0
robert,work,10
tom,running,8
tom,eating,5
tom,sleeping,9
tom,drinking,1
tom,work,10
cat,running,3
cat,eating,3
cat,drinking,2
cat,work,9
peter,running,3
peter,eating,2
peter,sleeping,10
peter,drinking,0
peter,work,10
john,hiking,99
john,work,88
$ 
$ # The program file
$ cat -n print_hoh_1.pl 
     1	#!/usr/bin/perl -w
     2	use strict;
     3	my ($result, $name, $activities, $value, $hdr_fmt, $row_fmt);
     4	
     5	# Subroutine section
     6	sub build_hoh {
     7	    ($name, $activities, $value) = @_;
     8	    $result->{$name}->{$activities} = $value;
     9	}
    10	
    11	sub build_fmt_strings {
    12	    my $aref = shift;
    13	    $hdr_fmt = "%10s|";
    14	    $row_fmt = "%-10s|";
    15	    foreach my $i (@{$aref}) {
    16	        $hdr_fmt .= "%-6s|";
    17	        $row_fmt .= "%6s|";
    18	    }
    19	    $hdr_fmt .= "\n";
    20	    $row_fmt .= "\n";
    21	}
    22	
    23	sub print_hoh {
    24	    my @values = ();
    25	    my @names = sort keys %{$result};
    26	    # Since the activities could vary across names, loop through
    27	    # all activities, collect them in all_activities hash and sort
    28	    my %all_activities;
    29	    foreach my $i (keys %{$result}) {
    30	        foreach my $j (keys %{$result->{$i}}) {
    31	            $all_activities{$j}++;
    32	        }
    33	    }
    34	    my @activities = sort keys %all_activities;
    35	    # Now build the format strings and start printing
    36	    build_fmt_strings(\@names);
    37	    printf($hdr_fmt, "", @names);
    38	    foreach my $act (@activities) {
    39	        foreach my $name (@names) {
    40	            push(@values, $result->{$name}->{$act} // "");
    41	        }
    42	        printf($row_fmt, $act, @values);
    43	        @values = ();
    44	    }
    45	}
    46	
    47	# Main section
    48	# We read the hash-of-hash data from a file and build our data structure
    49	my $file = $ARGV[0];
    50	open(FH, "<", $file) or die "Can't open $file: $!";
    51	while (<FH>) {
    52	   chomp(my $line = $_);
    53	   build_hoh(split(/,/, $line));
    54	}
    55	close(FH) or die "Can't close $file: $!";
    56	print_hoh;
    57	
$ 
$ 
$ # Run the Perl program
$ perl print_hoh_1.pl hoh_data.txt
          |cat   |john  |peter |robert|tom   |
drinking  |     2|      |     0|     0|     1|
eating    |     3|      |     2|    20|     5|
hiking    |      |    99|      |      |      |
running   |     3|      |     3|    10|     8|
sleeping  |      |      |    10|     9|     9|
work      |     9|    88|    10|    10|    10|
$ 
$ 
$ # Now add a new name and new activity and test
$ 
$ echo "wendy,yelling,55" >> hoh_data.txt
$ 
$ perl print_hoh_1.pl hoh_data.txt
          |cat   |john  |peter |robert|tom   |wendy |
drinking  |     2|      |     0|     0|     1|      |
eating    |     3|      |     2|    20|     5|      |
hiking    |      |    99|      |      |      |      |
running   |     3|      |     3|    10|     8|      |
sleeping  |      |      |    10|     9|     9|      |
work      |     9|    88|    10|    10|    10|      |
yelling   |      |      |      |      |      |    55|
$ 
$ 
$ # Associate the new name with an existing activity and test
$ 
$ echo "wendy,running,66" >> hoh_data.txt
$ 
$ perl print_hoh_1.pl hoh_data.txt
          |cat   |john  |peter |robert|tom   |wendy |
drinking  |     2|      |     0|     0|     1|      |
eating    |     3|      |     2|    20|     5|      |
hiking    |      |    99|      |      |      |      |
running   |     3|      |     3|    10|     8|    66|
sleeping  |      |      |    10|     9|     9|      |
work      |     9|    88|    10|    10|    10|      |
yelling   |      |      |      |      |      |    55|
$ 
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Hash Table

I was looking at this script and was wondering if anyone can explain what this script does and how does it work. Thank you for any help. State* lookup(char* prefix, int create) { int i, h; State *sp = NULL ; h = hash(prefix); for (sp = statetab; sp != NULL; sp... (14 Replies)
Discussion started by: totoro125
14 Replies

2. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Programming

Hash table

Hi, I hope someone can help me with the following prob.. I need to implement a hashtable whose KEYs are strings and VLAUEs are again hashtables. ie key - is a string and value -is another hashtable . So.... how am I supposed to be implementing my nested hashtable? Thanks in advance (1 Reply)
Discussion started by: andrew.paul
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

8. UNIX for Dummies Questions & Answers

nested hash table

Hi, I have a nested hash table say for example as follows: %coins = ( 1 => { "Quarter"=>25, "Dime"=>10, "Nickel"=>5, }, 2 => { "asd"=>34, "qwe"=>45, ... (0 Replies)
Discussion started by: arthi
0 Replies

9. Programming

Creating a Hash Table

Dear Friends, I want to create a hash table using the standard Glib header (if possible) so that I can store a structure and keep the hash key(search key) based on a string. Any example code would be great since I am not able to get the main idea. best regards Skull (4 Replies)
Discussion started by: callmetheskull
4 Replies

10. Programming

hash table implementations in C Language

Hello List, Iam searching for a solution where i can use hash based searching . In Detail , I have linked list which will be dynamically increasing . I need a best searching mechanisim such a way that it can take only one itereation . Right now iam using linear search which is taking... (11 Replies)
Discussion started by: vlrk
11 Replies
Login or Register to Ask a Question