免费注册 查看新帖 |

Chinaunix

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

关于散列使用问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-14 15:33 |只看该作者 |倒序浏览
比如我想把如下数据放入散列中
11/12/2009
吃1   0.30
玩2   1.40
我设计我的数据结构为一个散列,这个散列以日期为key,值为一个散列引用,这个散列的内容应该是 ( 吃1=> 0.30,玩2=>1.40 )
但是我在填数据的时候发现,有些数据打印出来是空值,不知道为什么

11/13/09 bill
Use of uninitialized value $ivalue in concatenation (.) or string at read_xls line 82.
==  ->                                        #这里有问题
== 测试2 -> 13.09
11/12/09 bill
== 测试1 -> 49
== 猪头 -> 12.99

我的测试文件内容为
11-12-09       
测试1        ¥12.99
猪头        ¥49
11-13-09       
测试2        ¥13.09


my %bill;
my $pointtmp;
my %day_bill;

for my $mydata ( $omydata->worksheets() )
{
  my ( $row_min, $row_max ) = $mydata->row_range();
  my ( $col_min, $col_max ) = $mydata->col_range();
   
     for my $row ( $row_min .. $row_max ) {
          my $cell = $mydata->get_cell( $row, 0 );
          next unless $cell;
# print "Row, Col    = ($row, $col)\n";

          my $tmp = $cell->value();
          $tmp=~s/\s+//ig;
          from_to($tmp,"CP936","utf8");

#print " match i find it \n" if $tmp eq "鐚?ご";

          # print $tmp;

          if( $tmp =~  m/\d+\/\d+/ )
          {
            if( %day_bill )
            {
               $bill{$pointtmp} = {%day_bill};
               %day_bill = undef;
            }
            #  $bill{$tmp} = undef;

            $pointtmp = $tmp;
            print "set date to $pointtmp\n";

            print "\n";
        }else{
          #print FH ":";

          $cell = $mydata->get_cell( $row, 1 );
          # print $cell->value(),"\n";

           $day_bill{$tmp}=$cell->value();
        }
     }
}
            $bill{$pointtmp} = {%day_bill};

            while(my($key,$value) = each(%bill))
            {
              print "$key bill \n";
              while(my($ikey,$ivalue) = each(%$value))
              {
                 print "== $ikey -> $ivalue \n";
             }
           }

论坛徽章:
0
2 [报告]
发表于 2009-12-14 15:59 |只看该作者
我把代码改了下,上面的写法有点怪异,是正常了,不过为什么上面的代码会有这样的输出结果呢?


for my $mydata ( $omydata->worksheets() )
{
  my ( $row_min, $row_max ) = $mydata->row_range();
  my ( $col_min, $col_max ) = $mydata->col_range();
   
     for my $row ( $row_min .. $row_max ) {
          my $cell = $mydata->get_cell( $row, 0 );
          next unless $cell;
          my $tmp = $cell->value();
          $tmp=~s/\s+//ig;
          from_to($tmp,"CP936","utf8");

          if( $tmp =~  m/\d+\/\d+/ )
          {
           
            $pointtmp = $tmp;
            print "set date to $pointtmp\n";

            print "\n";
        }else{
          
          $cell = $mydata->get_cell( $row, 1 );
          
          ${$bill{$pointtmp}}{$tmp} = $cell->value();
         
        }
     }
}


[ 本帖最后由 justlooks 于 2009-12-14 16:01 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-12-14 17:06 |只看该作者

回复 #1 justlooks 的帖子

you should know something about reference hash with HoH data structure.
HoH -- Hash of Hash:
i.e.; in your case should be:
%billing = (    # hash definition
    # date as key => reference hash as value
    '11-12-09'  => { '测试1' => 12.99,  '猪头'   => 49, },
   # record 2 ....
);

In your foreach loop of Excel's cell, you can do this:
1. fetching cells from (rows, 1st column) to match date as $date
2. fetching cells from (rows, 1st column) to match 测试1,猪头,... as $subkey
3. fetching cells from (rows, 2nd column) to match price, ... as $subvalue
finally put them into HoH:
$billing{$date}->{$subkey} = $subvalue;

try again!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP