Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Converting HOH to a XML not working

by sriram83.life (Acolyte)
on Mar 14, 2014 at 18:11 UTC ( [id://1078376]=perlquestion: print w/replies, xml ) Need Help??

sriram83.life has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I wrote a script that generates hash of hashes as follows:
$packagehash = { ' IBM Tivoli Runtime Security Services Client ' => { 'Publis +her' => 'IBM', 'Versio +n' => ' 7.1.0.2 ', 'Produc +tCode' => ' com.ibm.rtss.client', 'Produc +tName' => ' IBM Tivoli Runtime Security Services Client ' }, ' IBM Tivoli Runtime Security Services Software Development +Kit ' => { + 'Publisher' => 'IBM', + 'Version' => ' 7.1.0.2 ', + 'ProductCode' => ' com.ibm.rtss.sdk', + 'ProductName' => ' IBM Tivoli Runtime Security Services So +ftware Development Kit ' + }, ' IBM Tivoli Runtime Security Services Server ' => { 'Publis +her' => 'IBM', 'Versio +n' => ' 7.1.0.2 ', 'Produc +tCode' => ' com.ibm.rtss.server', 'Produc +tName' => ' IBM Tivoli Runtime Security Services Server ' } };
I am sending this hash reference $packagehash to a function which generates my desired xml file. I call the function as
&getxml($packagehash);
My logic in the function to retrieve inner hash key values i.e 'ProductName','ProductCode','Version' and 'Publisher' is not working.Output is blank. Here is the code i wrote:
sub getxml($) { my $hashref = shift; foreach my $key ( keys %${hashref} ){ print $key->{'ProductName'} print $key->{'ProductCode'} print $key->{'Publisher'} ...xml logic... }
When i print just $key, it prints outer hash keys,which is correct i.e productnames.But,if i want to extract their corresponding inner hash key and values,Output is showing blank.Please help me where i am making mistake.
Thanks,
Sriram

Replies are listed 'Best First'.
Re: Converting HOH to a XML not working
by kcott (Archbishop) on Mar 14, 2014 at 19:01 UTC

    G'day Sriram,

    "When i print just $key, it prints outer hash keys,which is correct i.e productnames."

    That doesn't seem right. Have you shown the real code you're using or are you incorrectly reporting the behaviour. With:

    foreach my $key ( %${hashref} ){ .... }

    Printing $key will give you something like:

    product name 1 HASH(0xffffffff) product name 2 HASH(0xffffffff)

    You need to start that loop more like this (i.e. include keys):

    for my $key (keys %$hashref) {
    "But,if i want to extract their corresponding inner hash key and values,Output is showing blank."

    To print your "inner" values, you'll need to change lines like this

    print $key->{'ProductName'}

    to be more like this

    print $hashref->{$key}{ProductName};

    Another issue is that your subroutine definition, "sub getxml($) {...}", includes a prototype (see "perlsub: Prototypes"). Unless you have a specific reason for using a prototype and really understand why you're using it, I recommend that you remove it, i.e. just use "sub getxml {...}". That's a general recommendation for all of your subroutine definitions, not just this one.

    In the previous paragraph I emphasised "really understand why you're using it". I did this because the only call you show to that subroutine, "&getxml($packagehash);", disables prototype checking (which tends to suggest that you don't really understand why you're using it). Again, unless you have a specific reason for doing this and really understand why you're doing it, I recommend that you call your subroutines without a leading ampersand (i.e. in this case, just use getxml($packagehash);). See "perlsub - Perl subroutines" for details.

    -- Ken

Re: Converting HOH to a XML not working
by toolic (Bishop) on Mar 14, 2014 at 18:25 UTC
    foreach my $key ( %${hashref} ){
    You should use keys:
    foreach my $key ( keys %${hashref} ){ print $hashref->{$key}->{'ProductName'};

    use strict and warnings catches such bugs.

      Hi Toolic,
      Actually it is a printing mistake.I gave keys %${hashref} only.I wrote exactly the same code you have specified,but still it is printing blank.
      Sriram
Re: Converting HOH to a XML not working
by clueless newbie (Curate) on Mar 14, 2014 at 18:35 UTC

    If you're doing this in the context to your earlier "How to return Hashref from one script to another script?", perhaps you should look at serialization. For example:

    #!/usr/bin/perl # parent use Data::Dumper; use strict; use warnings; my $stored=`perl child.pl`; my $data; eval $stored; warn Data::Dumper->Dump([\$data],[qw(*data)]);
    #!/usr/bin/perl # child use Data::Dumper; use strict; use warnings; my $data={aref=>[qw(a b c d)],href=>{}}; print Data::Dumper->Dump([\$data],[qw(*data)]);
      Thanks clueless newbie.
Re: Converting HOH to a XML not working
by hazylife (Monk) on Mar 14, 2014 at 18:27 UTC
    foreach my $key ( keys %$hashref ){ print $hashref->{$key}{ProductName}; print $hashref->{$key}{ProductCode}; print $hashref->{$key}{Publisher}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1078376]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found