免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1215 | 回复: 4
打印 上一主题 下一主题

又一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-03 16:07 |只看该作者 |倒序浏览
我期望碰到 OPTIONS的字符,而且它的值为 ADD 会答应对应的 ADD 动作,但是好像不行阿,看了下语句没错阿

for my $href (@hash_array){
  print "{";
  for my $role ( keys %$href ){
    print "$role = $href->{$role} ";
  }
  print "}\n";

for my $href (@hash_array){
    for my $role (keys %$href){
       if($role eq "OPTIONS" && $href->{$role} eq "ADD"){
            print "OK, i will do ADD action\n";
       }
    }
  }

输出
{backup_intval = 4
needbackup = world.backup_define
OPTIONS = ADD
srcdir = /var/lib/mysql
name = test_insert
fromip = 172.16.2.10
desdir = /data/backup
toip = 172.16.1.111
}
{backup_intval = 4
needbackup = world.backup_define
OPTIONS = ADD
srcdir = /var/lib/mysql
name = test_insert2
fromip = 172.16.2.10
desdir = /data/backup
toip = 172.16.1.111
}
{backup_intval = 4
needbackup = world.backup_define
OPTIONS = ADD
srcdir = /var/lib/mysql
name = test_insert4
fromip = 172.16.2.10
desdir = /data/backup
toip = 172.16.1.111
}

论坛徽章:
0
2 [报告]
发表于 2009-11-03 16:16 |只看该作者
你还是把所有代码都给贴出来吧
还有你push进去一个散列引用之后最好把原始散列给清空,不然有问题

论坛徽章:
0
3 [报告]
发表于 2009-11-03 16:23 |只看该作者
if(-e $CONFIG_DIR."/".$CONFIG_NAME){
  print "read $CONFIG_NAME\n";
  my @records = read_config_file($CONFIG_DIR."/".$CONFIG_NAME);  # read_config_file的功能是返回一个散列数组,其中所有数据都在这个数组中了
print "print records\n";
for my $href (@records){
  print "{";
  for my $role ( keys %$href ){
    print "$role = $href->{$role} ";
  }
  print "}\n";
}

for my $href (@records){
    for my $role (keys %$href){
       if($role eq "OPTIONS" && $href->{$role} eq "ADD"){
        print "OK, i will do ADD action\n";
      }   
    }   
}

sub read_config_file {
  my @hash_array;
  my $fl = shift;
  my ($volume,$path,$file) = File::Spec->splitpath( $fl );
  chdir $path or die "can not change dir to $path\n";
  open(FH,"<",$file) or die "can not open file\n";

    my %ha;
  while(<FH>){
    next if m/^#/;  # next if comment
    if(m/^$/){      # next if nothing ,and push %ha if have key in it
     if(keys %ha){
     push @hash_array,{%ha};
    }
     next;
  }
    my ($key ,$value) = split /\s:\s/;
    $ha{$key} = $value;
   }
close(FH);
return  @hash_array;


[ 本帖最后由 justlooks 于 2009-11-3 16:26 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-11-03 16:29 |只看该作者

回复 #1 justlooks 的帖子

你把结构dump一下,就会发现是
ADD\n
而不是ADD
所有的value都存在这个问题
解决的方法很简单,split之前先chomp一下

  1. use strict;
  2. use warnings;
  3. my @hash_array;
  4. my %ha;
  5. while (<DATA>) {
  6.     next if /^\s*#/;    # next if comment
  7.     chomp;
  8.     if (m/^$/) {        # next if nothing ,and push %ha if have key in it
  9.         if ( keys %ha ) {
  10.             push @hash_array, {%ha};
  11.             %ha = ();
  12.         }
  13.         next;
  14.     }
  15.     my ( $key, $value ) = split /\s:\s/;
  16.     $ha{$key} = $value;
  17. }

  18. #  print $#hash_array;

  19. =pod
  20. for my $href (@hash_array) {
  21.     print "{";
  22.     for my $role ( keys %$href ) {
  23.         print "$role = $href->{$role} ";
  24.     }
  25.     print "}\n";
  26. }
  27. =cut

  28. for my $href (@hash_array) {
  29.     for my $role ( keys %$href ) {
  30.         if ( ( $role eq "OPTIONS" ) and ( $href->{$role} eq "ADD" ) ) {
  31.             print "OK, i will do ADD action\n";
  32.         }
  33.     }
  34. }
  35. __DATA__
  36. backup_intval : 4
  37. needbackup : world.backup_define
  38. OPTIONS : ADD
  39. srcdir : /var/lib/mysql
  40. name : test_insert
  41. fromip : 172.16.2.10
  42. desdir : /data/backup
  43. toip : 172.16.1.111

  44. backup_intval : 4
  45. needbackup : world.backup_define
  46. OPTIONS : ADD
  47. srcdir : /var/lib/mysql
  48. name : test_insert2
  49. fromip : 172.16.2.10
  50. desdir : /data/backup
  51. toip : 172.16.1.111

  52. backup_intval : 4
  53. needbackup : world.backup_define
  54. OPTIONS : ADD
  55. srcdir : /var/lib/mysql
  56. name : test_insert4
  57. fromip : 172.16.2.10
  58. desdir : /data/backup
  59. toip : 172.16.1.111
复制代码

论坛徽章:
0
5 [报告]
发表于 2009-11-04 07:39 |只看该作者
原来是这样,知道了,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP