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

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

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:

  1. $VAR1 = {
  2. 'state1' => {
  3. 'code' => '328',
  4. 'num' => '179237'
  5. },
  6. 'state2' => {
  7. 'code' => '987',
  8. 'num' => '0.8736'
  9. },
  10. 'state3' => {
  11. 'code' => '326',
  12. 'num' => '582048'
  13. }
  14. };

The xml format I need it to be written is:

  1. <Root>
  2. <Info>
  3. <statename>state1</statename>
  4. <code>328<code>
  5. <num>179237<num>
  6. </Info>
  7. <Info>
  8. <statename>state2</statename>
  9. <code>987<code>
  10. <num>0.8736<num>
  11. </Info>
  12. <Info>
  13. <statename>state3</statename>
  14. <code>326<code>
  15. <num>582048<num>
  16. </Info>
  17. </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中访问一个哈希中的值,而这些值本身又是键值对。

  1. $size = keys %my_hash;
  2. for (my $i=0; $i<=$size; $i++){
  3. my $tags = $doc->createElement('Info');
  4. my %tags =
  5. statename =>
  6. code =>
  7. num =>
  8. }
  9. for my $state_name (sort keys %my_hash){
  10. my $state_tag = $doc->createElement ($statename);
  11. my $codetag->appendTextNode($code);
  12. $num = ->appendChild($statetag)
  13. }

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 :

  1. $VAR1 = {
  2. &#39;state1&#39; =&gt; {
  3. &#39;code&#39; =&gt; &#39;328&#39;,
  4. &#39;num&#39; =&gt; &#39;179237&#39;
  5. },
  6. &#39;state2&#39; =&gt; {
  7. &#39;code&#39; =&gt; &#39;987&#39;,
  8. &#39;num&#39; =&gt; &#39;0.8736&#39;
  9. },
  10. &#39;state3&#39; =&gt; {
  11. &#39;code&#39; =&gt; &#39;326&#39;,
  12. &#39;num&#39; =&gt; &#39;582048&#39;
  13. }
  14. };

The xml format I need it to be written is

  1. &lt;Root&gt;
  2. &lt;Info&gt;
  3. &lt;statename&gt;state1&lt;/statename&gt;
  4. &lt;code&gt;328&lt;code&gt;
  5. &lt;num&gt;179237&lt;num&gt;
  6. &lt;/Info&gt;
  7. &lt;Info&gt;
  8. &lt;statename&gt;state2&lt;/statename&gt;
  9. &lt;code&gt;987&lt;code&gt;
  10. &lt;num&gt;0.8736&lt;num&gt;
  11. &lt;/Info&gt;
  12. &lt;Info&gt;
  13. &lt;statename&gt;state3&lt;/statename&gt;
  14. &lt;code&gt;326&lt;code&gt;
  15. &lt;num&gt;582048&lt;num&gt;
  16. &lt;/Info&gt;
  17. &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中访问一个哈希中的值,而这些值本身又是键值对。

  1. $size = keys %my_hash;
  2. for(my $i=0; $&lt;=$size;$i++){
  3. my $tags = $doc-&gt;createElement(&#39;Info&#39;);
  4. my %tags =
  5. statename =&gt;
  6. code =&gt;
  7. num =&gt;
  8. }
  9. for my $state_name (sort keys %my_hash){
  10. my $state_tag = $doc-&gt;createElement ($statename);
  11. my $codetag-&gt;appendTextNode($code);
  12. $num = -&gt;appendChild($statetag)
  13. }

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

答案1

得分: 4

  1. #! /usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use XML::LibXML;
  5. my %hash = ('state1' => {
  6. 'code' => '328',
  7. 'num' => '179237'
  8. },
  9. 'state2' => {
  10. 'code' => '987',
  11. 'num' => '0.8736'
  12. },
  13. 'state3' => {
  14. 'code' => '326',
  15. 'num' => '582048'
  16. });
  17. my $dom = 'XML::LibXML::Document'->new('1.0', 'UTF-8');
  18. $dom->setDocumentElement(my $root = $dom->createElement('root'));
  19. for my $state (sort keys %hash) {
  20. my $info = $root->addNewChild("", 'Info');
  21. $info->addNewChild("", 'statename')->appendText($state);
  22. for my $key (qw( code num )) {
  23. $info->addNewChild("", $key)->appendText($hash{$state}{$key});
  24. }
  25. $info->appendText("\n");
  26. }
  27. print $dom;
英文:
  1. #! /usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use XML::LibXML;
  5. my %hash = (&#39;state1&#39; =&gt; {
  6. &#39;code&#39; =&gt; &#39;328&#39;,
  7. &#39;num&#39; =&gt; &#39;179237&#39;
  8. },
  9. &#39;state2&#39; =&gt; {
  10. &#39;code&#39; =&gt; &#39;987&#39;,
  11. &#39;num&#39; =&gt; &#39;0.8736&#39;
  12. },
  13. &#39;state3&#39; =&gt; {
  14. &#39;code&#39; =&gt; &#39;326&#39;,
  15. &#39;num&#39; =&gt; &#39;582048&#39;
  16. });
  17. my $dom = &#39;XML::LibXML::Document&#39;-&gt;new(&#39;1.0&#39;, &#39;UTF-8&#39;);
  18. $dom-&gt;setDocumentElement(my $root = $dom-&gt;createElement(&#39;root&#39;));
  19. for my $state (sort keys %hash) {
  20. my $info = $root-&gt;addNewChild(&quot;&quot;, &#39;Info&#39;);
  21. $info-&gt;addNewChild(&quot;&quot;, &#39;statename&#39;)-&gt;appendText($state);
  22. for my $key (qw( code num )) {
  23. $info-&gt;addNewChild(&quot;&quot;, $key)-&gt;appendText($hash{$state}{$key});
  24. }
  25. $info-&gt;appendText(&quot;\n&quot;);
  26. }
  27. 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:

确定