Sponsored Content
Top Forums Shell Programming and Scripting Need to print hash of hash in table format Post 302986036 by durden_tyler on Friday 18th of November 2016 06:54:57 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
LEXNAMES(5WN)						      WordNettm File Formats						     LEXNAMES(5WN)

NAME
lexnames - List of WordNet lexicographer file names and numbers DESCRIPTION
During WordNet development synsets are organized into forty-five lexicographer files based on syntactic category and logical groupings. grind(1WN) processes these files and produces a database suitable for use with the WordNet library, interface code, and other applications. The format of the lexicographer files is described in wninput(5WN). A file number corresponds to each lexicographer file. File numbers are encoded in several parts of the WordNet system as an efficient way to indicate a lexicographer file name. The file lexnames lists the mapping between file names and numbers, and can be used by programs or end users to correlate the two. File Format Each line in lexnames contains 3 tab separated fields, and is terminated with a newline character. The first field is the two digit deci- mal integer file number. (The first file in the list is numbered 00.) The second field is the name of the lexicographer file that is rep- resented by that number, and the third field is an integer that indicates the syntactic category of the synsets contained in the file. This is simply a shortcut for programs and scripts, since the syntactic category is also part of the lexicographer file's name. Syntactic Category The syntactic category field is encoded as follows: 1 NOUN 2 VERB 3 ADJECTIVE 4 ADVERB Lexicographer Files The names of the lexicographer files and their corresponding file numbers are listed below along with a brief description each file's con- tents. File Number Name Contents ------------------------------------------------------------------------------------------------ 00 adj.all all adjective clusters 01 adj.pert relational adjectives (pertainyms) 02 adv.all all adverbs 03 noun.Tops unique beginner for nouns 04 noun.act nouns denoting acts or actions 05 noun.animal nouns denoting animals 06 noun.artifact nouns denoting man-made objects 07 noun.attribute nouns denoting attributes of people and objects 08 noun.body nouns denoting body parts 09 noun.cognition nouns denoting cognitive processes and contents 10 noun.communication nouns denoting communicative processes and contents 11 noun.event nouns denoting natural events 12 noun.feeling nouns denoting feelings and emotions 13 noun.food nouns denoting foods and drinks 14 noun.group nouns denoting groupings of people or objects 15 noun.location nouns denoting spatial position 16 noun.motive nouns denoting goals 17 noun.object nouns denoting natural objects (not man-made) 18 noun.person nouns denoting people 19 noun.phenomenon nouns denoting natural phenomena 20 noun.plant nouns denoting plants 21 noun.possession nouns denoting possession and transfer of possession 22 noun.process nouns denoting natural processes 23 noun.quantity nouns denoting quantities and units of measure 24 noun.relation nouns denoting relations between people or things or ideas 25 noun.shape nouns denoting two and three dimensional shapes 26 noun.state nouns denoting stable states of affairs 27 noun.substance nouns denoting substances 28 noun.time nouns denoting time and temporal relations 29 verb.body verbs of grooming, dressing and bodily care 30 verb.change verbs of size, temperature change, intensifying, etc. 31 verb.cognition verbs of thinking, judging, analyzing, doubting 32 verb.communication verbs of telling, asking, ordering, singing 33 verb.competition verbs of fighting, athletic activities 34 verb.consumption verbs of eating and drinking 35 verb.contact verbs of touching, hitting, tying, digging 36 verb.creation verbs of sewing, baking, painting, performing 37 verb.emotion verbs of feeling 38 verb.motion verbs of walking, flying, swimming 39 verb.perception verbs of seeing, hearing, feeling 40 verb.possession verbs of buying, selling, owning 41 verb.social verbs of political and social activities and events 42 verb.stative verbs of being, having, spatial relations 43 verb.weather verbs of raining, snowing, thawing, thundering 44 adj.ppl participial adjectives NOTES
The lexicographer files are not included in the WordNet database package. ENVIRONMENT VARIABLES (UNIX) WNHOME Base directory for WordNet. Default is /usr/local/WordNet-3.0. WNSEARCHDIR Directory in which the WordNet database has been installed. Default is WNHOME/dict. REGISTRY (WINDOWS) HKEY_LOCAL_MACHINESOFTWAREWordNet3.0WNHome Base directory for WordNet. Default is C:Program FilesWordNet3.0. FILES
lexnames list of lexicographer file names and numbers SEE ALSO
grind(1WN), wnintro(5WN), wndb(5WN), wninput(5WN). WordNet 3.0 Dec 2006 LEXNAMES(5WN)
All times are GMT -4. The time now is 03:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy