CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [FishMonger] Can we use Dumper to define hash of hash of hashes , rather than we manually creating it.: Edit Log



Tejas
User

Jan 9, 2015, 9:16 AM


Views: 2321
Re: [FishMonger] Can we use Dumper to define hash of hash of hashes , rather than we manually creating it.

Hi FishMonger

I dont think tht will work for the requirement i have.
I have just asked the doubt that is part of my requirements.

Let me explain the full details.
The data is still dummy


Quote
So , The Original Requirement and the reason behind me wanting to generate the definintion of hash through script is metioned in code below

All these are examples and they relate to my original requirement, so descriptions here have similar values, but in reality each description each key is a unique and a different value.



Code
 
my %description1 = (1=>abc1,2=>xyz1,3=>PQR1,4=>Blah1);
my %description2 = (1=>abc2,2=>xyz2,3=>PQR2,4=>Blah2);
my %description3 = (111=>abc3,112=>xyz3,113=>PQR3,114=>Blah3);
my %description4 = (1=>abc4,2=>xyz4,3=>PQR4,4=>Blah4);
my %description5 = (1=>abc5,2=>xyz5,3=>PQR5,4=>Blah5);
my %description6 = (1=>abc6,2=>xyz6,3=>PQR6,4=>Blah6);
my %description7 = (1=>abc7,2=>xyz7,3=>PQR7,4=>Blah7);


my %data_hash ;
$data_hash{1}{1}{111}{1}{1}{1}{1} = ["04-MSL-1"];
$data_hash{1}{1}{111}{1}{1}{1}{2} = ["04-MSL-47"];
$data_hash{1}{1}{111}{1}{1}{1}{3} = ["04-MSL-204"];
$data_hash{1}{1}{111}{1}{1}{1}{4} = [""];
$data_hash{1}{1}{111}{1}{1}{1}{5} = [""];
$data_hash{1}{1}{111}{1}{1}{1}{6} = [""];
$data_hash{1}{1}{111}{1}{1}{1}{7} = [""];
$data_hash{1}{1}{111}{1}{1}{1}{8} = [""];
$data_hash{1}{1}{111}{1}{1}{1}{9} = ["04-MSL-526"];
$data_hash{1}{1}{111}{1}{1}{1}{10} = ["04-MSL-68"];


Quote
Now these numbers would be printed sequentially and each key woule me matched with a hash which has description and there are 7 levels
i.e something like below.


Code
oreach my $l1 ( sort {$a <=> $b}keys %data_hash) { 
foreach my $l2 ( sort {$a <=> $b} keys %{$data_hash{$l1}}) {
foreach my $l3 ( sort {$a <=> $b} keys %{$data_hash{$l1}{$l2}}) {
foreach my $l4 ( sort {$a <=> $b} keys %{$data_hash{$l1}{$l2}{$l3}}) {
foreach my $l5 ( sort {$a <=> $b} keys %{$data_hash{$l1}{$l2}{$l3}{$l4}}) {
foreach my $l6 ( sort {$a <=> $b} keys %{$data_hash{$l1}{$l2}{$l3}{$l4}{$l5}}) {
foreach my $l7 (sort {$a <=> $b} keys %{$data_hash{$l1}{$l2}{$l3}{$l4}{$l5}{$l6}}) {

my @gl_data = ($description1{$l3},$description2{$l4},$description3{$l5},$description4{$l6});
my @sl_data = $data_hash{$l1}{$l2}{$l3}{$l4}{$l5}{$l6}{$l7};
print "@gl_data,@sl_data \n";
# print_to_excel (\@gl_data,\@sl_data);

}}}}}}}

#These hashes are not full in content.just for the purpose of understanding



Quote
Above is the way we are hardcodiing the hash with numbers correspoding to some description,which are decoded during print.

Now, Coming to the main motto behind this thread,
I have an input file with the descriptions

So that the print order is in my hands and the order is determined the data hash keys.
While i loop , it ll be in order(numerical).


abc1,abc2,abc3,abc4,PQR5,abc6,Blah7,04,MSL,68
and this is hard coded as
$data_hash{1}{1}{111}{1}{3}{1}{4} = ["04-MSL-68"];

And we have tried to create hash of hash of hash to an array
This way i have lots of lines and i have to hard code a data hash with corresonding number keys.

And due to the same reason , i wanted the perl dumper to create the definition .
As i have lots and lots of lines , and they should be defined by dumper and shoiuld nt be literal values like we have initially tried, but should b ethe hash key
(number) to which this value is created
ex : my %description7 = (1=>abc7,2=>xyz7,3=>PQR7,4=>Blah7);
Though we have abc7 in the file, hash should have 1 and i will decode it to abc7 while prinitng it.



Now , all iam trying to do is
1. read the file with values
2. get the key from the descripion hash for the read value
3.Create a data hash with keys.
ex :
File data :
abc1,abc2,abc3,abc4,PQR5,abc6,Blah7,04,MSL,68
abc1,abc2,abc3,abc4,PQR5,abc6,Blah7,04,MSL,69
$data_hash{1}{1}{111}{1}{3}{1}{4} = ["04-MSL-68","04-MSL-69"];
Hope this is clear.


Thanks
Tejas


(This post was edited by Tejas on Jan 9, 2015, 9:38 AM)


Edit Log:
Post edited by Tejas (User) on Jan 9, 2015, 9:19 AM
Post edited by Tejas (User) on Jan 9, 2015, 9:38 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives