Printing hash in table format

D

Deepu

I am trying to print hash of hashes in table format on to the screen or

to a file. But am not able to get how this can be achieved. Can
somebody please help me on this.

Now i am printing a hash of hashes using:

while (($key, $value) = each %hash) {
print ("%-20s => %d\n", $key, $value);

}

The keys and values is actually:

"TEST1-TEST2" => '2'
"TEST2-TEST3" => '3'
"TEST1-TEST3" => '1'
"TEST2-TEST1" => '0'

I need to put the key and values in atable format like:


STATES TEST1 TEST2 TEST3


TEST1 2 1 3


TEST2 1 0 0


TEST3 4 6 3

Background:

There is actually a main table (in a file) with the static transitions
like:


STATES TEST1 TEST2 TEST3 TEST4
TEST1 Y N N Y
TEST2 N Y Y N
TEST3 N N N Y
TEST4 Y Y Y N


Y -Possible
N - Not possible
I need to count how many possible and not possible transitions have
occured with respect tp each transition.
and i will also have another file which will have some actual
transitions like:


TEST1
TEST3
TEST2
TEST1
TEST4


(This actually meant the present state is TEST1 then next state is
TEST3, then the present state is TEST3 and next state is TEST2 and so
on).


What i did was put the main table in a hash and also the actual
transitions file in a hash, compare both and print out the hash like


%hash = ( "TEST1-TEST2" => 0
"TEST2-TEST3" => 1
"TEST3-TEST4" => 2
"TEST4-TEST5" => 1
)


The numbers indicate how many times the transition has occured in
actual transition file.


Now i need to put this hash again in a table format.


STATES TEST1 TEST2 TEST3


TEST1 2 1 3


TEST2 1 0 0


TEST3 4 6 3


Thanks
Pradeep
 
H

Hobo Salesman

Deepu said:
I am trying to print hash of hashes in table format on to the screen or

to a file. But am not able to get how this can be achieved. Can
somebody please help me on this.

Theres a lot more skilled people here than me but I can tell you how
I'd approach it anyway. I skimmed over your post and it looks like
basically you want to read tables into data structures, analyze that
data, and write them back out to tables. It's possible theres a module
that does it but I don't know of it.

THeres a lot of ways to structure your data that would depend on what
you want to do with it and how consistently it's structured. Read this:

http://search.cpan.org/dist/perl/pod/perldsc.pod
http://search.cpan.org/dist/perl/pod/perllol.pod

If everything is actually in a nice consistent order (test1, test2,
test3) like that then it should be pretty easy to read files line by
line into a structure you can work with. To count boolean values in an
entry just loop through the array/hash for that entry and set a count
variable for Y and/or N.

Hope some of that helped.

HS
 
A

Anno Siegel

Deepu said:
I am trying to print hash of hashes in table format on to the screen or

to a file. But am not able to get how this can be achieved. Can
somebody please help me on this.

Now i am printing a hash of hashes using:

while (($key, $value) = each %hash) {
print ("%-20s => %d\n", $key, $value);

}

The code looks like you're printing a plain hash, not a hash of
hashes. Otherwise, $value would be a hash ref, which can't be
reasonably printed as a number.
The keys and values is actually:

"TEST1-TEST2" => '2'
"TEST2-TEST3" => '3'
"TEST1-TEST3" => '1'
"TEST2-TEST1" => '0'

I need to put the key and values in atable format like:


STATES TEST1 TEST2 TEST3


TEST1 2 1 3


TEST2 1 0 0


TEST3 4 6 3

Please explain how you get from the first table to this one. I don't
see the connection.

Anno
 
D

Deepu

Hi All,

Once i have the 1st main table as input i read the table and store it
in a hash with keys and values
"TEST1-TEST2" => 1
"TEST1-TEST3" => 0

1 for Y in the main table and 0 for N in the main table.

I also have a 2nd hash with same keys but values will be initialized to
zeros.

Then i will read the 2nd file with transitions, store it in an array
and for each element compare with the 1st hash. If the transition
exists then increment the value of 2nd hash pertaining to that key.

Once i have all the keys and values, i will print using the part of
code shown before. But now i am trying to print it in table format
instead of just printing like hash.

Thanks for the help.
 
H

Hobo Salesman

Deepu said:
Once i have the 1st main table as input i read the table and store it
in a hash with keys and values
"TEST1-TEST2" => 1
"TEST1-TEST3" => 0

Seems to me that's a real bad way to store your data. Now you have to
split a string into two fields in order to do anything useful.

STATES TEST1 TEST2 TEST3
TEST1 2 1 3
TEST2 1 0 0
TEST3 4 6 3

my $index = {
'STATES' => 0,
'TEST1' => 1,
'TEST2' => 2,
'TEST3' => 3,
};

my $table = [
[ "TEST1", 2, 1, 3 ],
[ "TEST2", 1, 0, 0 ],
[ "TEST3", 4, 6, 3 ],
];

#prints TEST1 field for second entry (1)
print $table->[1][$index->{"TEST1"}];

#prints the table
foreach my $entry (@$table) {
print join(" ", @$entry)."\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top