如何在Perl中访问一个哈希中的值,而这些值本身又是键值对。

huangapple go评论52阅读模式
英文:

how to access values of a hash which are again a key, value pairs in perl

问题

I have a hash like below, I need to access it and put that info in an xml. I couldn't access properly. I am facing difficulty in understanding and using hash datatype.

Hash that needs to be referred:

$VAR1 = {
          'state1' => {
                        'code' => '328',
                        'num' => '179237'
                      },
          'state2' => {
                        'code' => '987',
                        'num' => '0.8736'
                      },
          'state3' => {
                        'code' => '326',
                        'num' => '582048'
                      }
        };

The xml format I need it to be written is:

<Root>

 <Info>
  <statename>state1</statename>
  <code>328<code>
  <num>179237<num>
  </Info>

 <Info>
  <statename>state2</statename>
  <code>987<code>
  <num>0.8736<num>
 </Info>

<Info>
  <statename>state3</statename>
  <code>326<code>
  <num>582048<num>
 </Info>
 
</Root>

I need to use XML::LibXML lib. Apologies for not posting the attempted codes from my end. Since I need it immediately and I am slow at accessing hashes, I am unable to provide any proper working code. Please help. 如何在Perl中访问一个哈希中的值,而这些值本身又是键值对。

$size = keys %my_hash; 
for (my $i=0; $i<=$size; $i++){
 my $tags = $doc->createElement('Info');
 my %tags = 
   statename => 
   code =>
   num => 
}

for my $state_name (sort keys %my_hash){
 my $state_tag = $doc->createElement ($statename);
 my $codetag->appendTextNode($code);
 $num = ->appendChild($statetag) 
}

Please also point to any documentation to learn from beginning to access hashes and use in XMLs.

英文:

I have a hash like below, I need to access it and put that info in an xml. I couldn't access properly. I am facing difficulty in understanding and using hash datatype.
Hash that needs to be referred :

$VAR1 = {
          &#39;state1&#39; =&gt; {
                        &#39;code&#39; =&gt; &#39;328&#39;,
                        &#39;num&#39; =&gt; &#39;179237&#39;
                      },
          &#39;state2&#39; =&gt; {
                        &#39;code&#39; =&gt; &#39;987&#39;,
                        &#39;num&#39; =&gt; &#39;0.8736&#39;
                      },
          &#39;state3&#39; =&gt; {
                        &#39;code&#39; =&gt; &#39;326&#39;,
                        &#39;num&#39; =&gt; &#39;582048&#39;
                      }
        };

The xml format I need it to be written is

&lt;Root&gt;

 &lt;Info&gt;
  &lt;statename&gt;state1&lt;/statename&gt;
  &lt;code&gt;328&lt;code&gt;
  &lt;num&gt;179237&lt;num&gt;
  &lt;/Info&gt;

 &lt;Info&gt;
  &lt;statename&gt;state2&lt;/statename&gt;
  &lt;code&gt;987&lt;code&gt;
  &lt;num&gt;0.8736&lt;num&gt;
 &lt;/Info&gt;

&lt;Info&gt;
  &lt;statename&gt;state3&lt;/statename&gt;
  &lt;code&gt;326&lt;code&gt;
  &lt;num&gt;582048&lt;num&gt;
 &lt;/Info&gt;
 
&lt;/Root&gt;

I need to use XML::LibXML lib, Apologies for not posting the attempted codes from my end. Since I need it immediately and I am slow at accessing hashes, I am unable to provide any proper working code. Please help. 如何在Perl中访问一个哈希中的值,而这些值本身又是键值对。

$size = keys %my_hash; 
for(my $i=0; $&lt;=$size;$i++){
 my $tags = $doc-&gt;createElement(&#39;Info&#39;);
 my %tags = 
   statename =&gt; 
   code =&gt;
   num =&gt; 
}

for my $state_name (sort keys %my_hash){
 my $state_tag = $doc-&gt;createElement ($statename);
 my $codetag-&gt;appendTextNode($code);
 $num = -&gt;appendChild($statetag) 
}

Please also point to any documentation to learn from beginning to access hashes and use in xmls.

答案1

得分: 4

#! /usr/bin/perl
use warnings;
use strict;

use XML::LibXML;

my %hash = ('state1' => {
                'code' => '328',
                'num' => '179237'
            },
            'state2' => {
                'code' => '987',
                'num' => '0.8736'
            },
            'state3' => {
                'code' => '326',
                'num' => '582048'
            });

my $dom = 'XML::LibXML::Document'->new('1.0', 'UTF-8');
$dom->setDocumentElement(my $root = $dom->createElement('root'));

for my $state (sort keys %hash) {
    my $info = $root->addNewChild("", 'Info');
    $info->addNewChild("", 'statename')->appendText($state);
    for my $key (qw( code num )) {
        $info->addNewChild("", $key)->appendText($hash{$state}{$key});
    }
    $info->appendText("\n");
}
print $dom;
英文:
#! /usr/bin/perl
use warnings;
use strict;

use XML::LibXML;

my %hash = (&#39;state1&#39; =&gt; {
                &#39;code&#39; =&gt; &#39;328&#39;,
                &#39;num&#39; =&gt; &#39;179237&#39;
            },
            &#39;state2&#39; =&gt; {
                &#39;code&#39; =&gt; &#39;987&#39;,
                &#39;num&#39; =&gt; &#39;0.8736&#39;
            },
            &#39;state3&#39; =&gt; {
                &#39;code&#39; =&gt; &#39;326&#39;,
                &#39;num&#39; =&gt; &#39;582048&#39;
            });

my $dom = &#39;XML::LibXML::Document&#39;-&gt;new(&#39;1.0&#39;, &#39;UTF-8&#39;);
$dom-&gt;setDocumentElement(my $root = $dom-&gt;createElement(&#39;root&#39;));

for my $state (sort keys %hash) {
    my $info = $root-&gt;addNewChild(&quot;&quot;, &#39;Info&#39;);
    $info-&gt;addNewChild(&quot;&quot;, &#39;statename&#39;)-&gt;appendText($state);
    for my $key (qw( code num )) {
        $info-&gt;addNewChild(&quot;&quot;, $key)-&gt;appendText($hash{$state}{$key});
    }
    $info-&gt;appendText(&quot;\n&quot;);
}
print $dom;

huangapple
  • 本文由 发表于 2023年2月13日 23:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定