免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: lybpyn
打印 上一主题 下一主题

怎么判断hash中指定的结构是否存在? [复制链接]

论坛徽章:
0
21 [报告]
发表于 2011-11-09 17:04 |只看该作者
回复 20# sosflyer
  1. #!/usr/bin/perl -w
  2. use XML::Simple;
  3. use Data::Dumper;
  4. use strict;

  5. my $xml = new XML::Simple;

  6. my $conf = $xml->XMLin("data.xml");
  7. my $i=0;
  8. my $j=0;
  9. my $m=0;
  10. my $d=0;
  11. my @agr;
  12. my @arr;

  13. my $filename="";
  14. my $filename2="";
  15. my $value=0;
  16. my $value2=0;
  17.          while($conf->{'measData'}->{'measInfo'}->[$i])
  18.        {
  19.                          $filename=$conf->{'measData'}->{'measInfo'}->[$i]->{'measInfoId'};
  20.                          my $tmp=$filename;
  21.                          $filename.="-measTypes.txt";
  22.                          open(fhn,">$filename")|| die $!;
  23.                          
  24.                          my $a=$conf->{'measData'}->{'measInfo'}->[$i]->{'measTypes'};
  25.                          my @arg=split/\s+/,$a;
  26.                          
  27.                          while(@arg)
  28.                    {
  29.                      $value=shift(@arg);
  30.                          $j ++;
  31.              print fhn ("$j\t$value\n");
  32.                    }
  33.            close (fhn);
  34.            
  35.                    $filename2=$tmp."-measResults.txt";
  36.            open(fhn2,">$filename2")|| die $!;
  37.                    #$d=exists $conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->[$m];
  38.                    if ( exists $conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->[$m])
  39.                    {
  40.                    my $c=$conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->[$m];
  41.                    while($conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->[$m]->{'measResults'})
  42.                    {
  43.            my $b=$conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->[$m]->{'measResults'};
  44.                    my @arr=split/\s+/,$b;
  45.                    while(@arr)
  46.                    {
  47.                            $value2=shift(@arr);
  48.                            print fhn2 ("\t$value2\n");
  49.                    }
  50.                    $m ++;
  51.                    }
  52.                    }
  53.                    else
  54.                    {
  55.                            while($conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->{'measResults'})
  56.                    {
  57.            my $b=$conf->{'measData'}->{'measInfo'}->[$i]->{'measValue'}->{'measResults'};
  58.                    my @arr=split/\s+/,$b;
  59.                    while(@arr)
  60.                    {
  61.                            $value2=shift(@arr);
  62.                            print fhn2 ("\t$value2\n");
  63.                    }
  64.                    }
  65.                    }
  66.                   
  67.                    close (fhn2);
  68.                    $i ++;
  69.                    $j =0;
  70.        }

  71.    
复制代码

论坛徽章:
0
22 [报告]
发表于 2011-11-09 17:07 |只看该作者
回复 20# sosflyer


    实际上用或者不用它都会提示报错 没区别

论坛徽章:
0
23 [报告]
发表于 2011-11-09 17:18 |只看该作者
本帖最后由 sosflyer 于 2011-11-09 17:24 编辑

回复 22# lybpyn


    囧,好像你53行这个m++位置有问题

论坛徽章:
0
24 [报告]
发表于 2011-11-09 17:29 |只看该作者
回复 23# sosflyer


    真不是哪的问题!就是因为没有找到所需结构才报错!m++位置没错他是要遍历xml中measValue节点下的所有值

论坛徽章:
0
25 [报告]
发表于 2011-11-09 20:54 |只看该作者
散列的键,不能使用引用。

论坛徽章:
0
26 [报告]
发表于 2011-11-10 09:00 |只看该作者
回复 25# Perlvim


    可以使用啊!怎么会不能用呢

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
27 [报告]
发表于 2011-11-10 10:11 |只看该作者
7楼: 常用Data::Dumper看看吧

看了就知道了....别再浪费时间了....

论坛徽章:
0
28 [报告]
发表于 2011-11-10 18:32 |只看该作者
回复 27# jason680


    我做xml解析需要判断一下 因为有些结构存在有些不存在  直接看结构没啥用

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
29 [报告]
发表于 2011-11-10 20:40 |只看该作者
It's up to you

论坛徽章:
0
30 [报告]
发表于 2011-11-10 21:44 |只看该作者
http://perldoc.perl.org/perlfaq4 ... ce-as-a-hash-key%3F

How can I use a reference as a hash key?

(contributed by brian d foy and Ben Morrow)

Hash keys are strings, so you can't really use a reference as the key. When you try to do that, perl turns the reference into its stringified form (for instance, HASH(0xDEADBEEF) ). From there you can't get back the reference from the stringified form, at least without doing some extra work on your own.

Remember that the entry in the hash will still be there even if the referenced variable goes out of scope, and that it is entirely possible for Perl to subsequently allocate a different variable at the same address. This will mean a new variable might accidentally be associated with the value for an old.

If you have Perl 5.10 or later, and you just want to store a value against the reference for lookup later, you can use the core Hash::Util::Fieldhash module. This will also handle renaming the keys if you use multiple threads (which causes all variables to be reallocated at new addresses, changing their stringification), and garbage-collecting the entries when the referenced variable goes out of scope.

If you actually need to be able to get a real reference back from each hash entry, you can use the Tie::RefHash module, which does the required work for you.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP