免费注册 查看新帖 |

Chinaunix

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

debug报错,是关于变量作用域的问题,附代码 [复制链接]

论坛徽章:
5
技术图书徽章
日期:2014-02-10 10:55:18技术图书徽章
日期:2014-03-17 16:37:45狮子座
日期:2014-04-25 11:17:42未羊
日期:2014-08-13 11:45:23天蝎座
日期:2015-12-16 10:30:37
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-31 13:33 |只看该作者 |倒序浏览
原帖由 godsad 于 2009-7-31 13:36 发表
@wordALL和$wordDiff应该作用于整个子程序啊,怎么还会报这两个变量是全局变量的警告?


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

open FILE,"c:\\zz.txt";
my @tmp = <FILE>;
close FILE;
print "Please enter the word.\n";
my %count=&diff(@tmp);
print "The word appeard ".$count{<STDIN>}."times in the file.\n";

sub diff {
    my $wordFirst;
    my @wordDiff;
    my @wordAll;
    my $sameNum;
    my $tmp1;
    my $tmp2;
    my %diff;

    foreach (@_){
              chomp;
              push @wordAll,$_;
              }

    $wordFirst=$wordAll[0];

    foreach (@wordAll){
              if ($wordFirst!=$_){
              push @wordDiff,$_;
              }
     }

    push @wordDiff,$wordFirst;

    foreach $tmp1(@wordDiff){
              foreach $tmp2(@wordALL){      #  报错:Global symbol "@wordALL" requires explicit package name at Untitled line 36.
                       if ($tmp1==$tmp2){
                          $sameNum++;
                          }
                       }
              $sameNum-=1; #self
              $diff{$wordDiff}=$sameNum;    #  报错:Global symbol "$wordDiff" requires explicit package name at Untitled line 42.
              }
    %diff;
}


[ 本帖最后由 godsad 于 2009-7-31 13:59 编辑 ]

论坛徽章:
5
技术图书徽章
日期:2014-02-10 10:55:18技术图书徽章
日期:2014-03-17 16:37:45狮子座
日期:2014-04-25 11:17:42未羊
日期:2014-08-13 11:45:23天蝎座
日期:2015-12-16 10:30:37
2 [报告]
发表于 2009-07-31 13:36 |只看该作者
@wordALL和$wordDiff应该作用于整个子程序啊,怎么还会报这两个变量是全局变量的警告?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2009-07-31 14:01 |只看该作者
原帖由 godsad 于 2009-7-31 13:36 发表
@wordALL和$wordDiff应该作用于整个子程序啊,怎么还会报这两个变量是全局变量的警告?

1. Perl 的变量名是区分大小写的, 也即 @wordALL 和 @wordAll 是不同的变量
2. 数组和标量不共享名字空间,也即 @wordDiff 和 $wordDiff 是不同的变量。

论坛徽章:
5
技术图书徽章
日期:2014-02-10 10:55:18技术图书徽章
日期:2014-03-17 16:37:45狮子座
日期:2014-04-25 11:17:42未羊
日期:2014-08-13 11:45:23天蝎座
日期:2015-12-16 10:30:37
4 [报告]
发表于 2009-07-31 14:10 |只看该作者
原帖由 MMMIX 于 2009-7-31 14:01 发表

1. Perl 的变量名是区分大小写的, 也即 @wordALL 和 @wordAll 是不同的变量
2. 数组和标量不共享名字空间,也即 @wordDiff 和 $wordDiff 是不同的变量。

谢谢,还有好多bug 呵呵

ok了hash练习

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;

  4. open FILE,"c:\\zz.txt";
  5. my @tmp = <FILE>;
  6. close FILE;
  7. print "Please enter the word.\n";
  8. my %count=&diff(@tmp);
  9. my @tmp1=<STDIN>;
  10. foreach (sort(@tmp1)){
  11.           chomp;
  12.           print "The word ".$_." appeard ".$count{$_}." times in the file.\n";
  13.           }

  14. sub diff {
  15.     my $wordFirst;
  16.     my @wordDiff;
  17.     my @wordAll;
  18.     my $sameNum;
  19.     my $tmp1;
  20.     my $tmp2;
  21.     my %diff;

  22.     foreach (@_){
  23.               chomp;
  24.               push @wordAll,$_;
  25.               }

  26.     $wordFirst=$wordAll[0];

  27.     foreach (@wordAll){
  28.               if ($wordFirst ne $_){
  29.               push @wordDiff,$_;
  30.               }
  31.     }

  32.     push @wordDiff,$wordFirst;

  33.     foreach $tmp1(@wordDiff){
  34.               foreach $tmp2(@wordAll){
  35.                        if ($tmp1 eq $tmp2){
  36.                           $sameNum++;
  37.                           }
  38.                        }
  39.               $diff{$tmp1}=$sameNum;
  40.               }
  41.     %diff;
  42.     }
复制代码

最终版,呵呵,看来以后要多写程序了

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;

  4. open FILE,"c:\\zz.txt";
  5. my @tmp = <FILE>;
  6. close FILE;
  7. print "Please enter the word.\n";
  8. my %count=&diff(@tmp);
  9. my @tmp1=<STDIN>;
  10. foreach (sort(@tmp1)){
  11.           chomp;
  12.           print "The word ".$_." appeard ".$count{$_}." times in the file.\n";
  13.           }

  14. sub diff {
  15.     my $wordFirst;
  16.     my @wordDiff;
  17.     my @wordAll;
  18.     my $sameNum;
  19.     my $tmp1;
  20.     my $tmp2;
  21.     my %diff;
  22.     my $tmp3;
  23.     my $tmp4;

  24.     foreach (@_){
  25.               chomp;
  26.               push @wordAll,$_;
  27.               }

  28.     $wordFirst=$wordAll[0];

  29.     push @wordDiff,$wordFirst;

  30.     foreach $tmp3(@wordAll){
  31.               if ($wordFirst ne $tmp3){
  32.                    foreach $tmp4(@wordDiff){
  33.                              if ($tmp3 ne $tmp4){
  34.                                   push @wordDiff,$tmp3;
  35.                              }
  36.                    }

  37.               }
  38.     }


  39.     foreach $tmp1(@wordDiff){
  40.               $sameNum=0;
  41.               foreach $tmp2(@wordAll){
  42.                        if ($tmp1 eq $tmp2){
  43.                           $sameNum++;
  44.                           }
  45.                        }
  46.               $diff{$tmp1}=$sameNum;
  47.               }
  48.     return %diff;

  49.     }
复制代码

完美版,嗯这题涵盖了很多东东...

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;

  4. open FILE,"c:\\zz.txt";
  5. my @tmp = <FILE>;
  6. close FILE;
  7. print "Please enter the word.\n";
  8. my %count=&diff(@tmp);
  9. my @tmp1=<STDIN>;
  10. foreach (sort(@tmp1)){
  11.           chomp;
  12.           if (exists $count{$_}){
  13.                 print "The word ".$_." appeard ".$count{$_}." times in the file.\n";
  14.           }
  15.           else {print "The word ".$_." can`nt find in the file.\n";}
  16. }
  17. sub diff {
  18.     my $wordFirst;
  19.     my @wordDiff;
  20.     my @wordAll;
  21.     my $sameNum;
  22.     my $tmp1;
  23.     my $tmp2;
  24.     my %diff;
  25.     my $tmp3;
  26.     my $tmp4;

  27.     foreach (@_){
  28.               chomp;
  29.               push @wordAll,$_;
  30.               }

  31.     $wordFirst=$wordAll[0];

  32.     push @wordDiff,$wordFirst;

  33.     foreach $tmp3(@wordAll){
  34.               if ($wordFirst ne $tmp3){
  35.                    foreach $tmp4(@wordDiff){
  36.                              if ($tmp3 ne $tmp4){
  37.                                   push @wordDiff,$tmp3;
  38.                              }
  39.                    }

  40.               }
  41.     }


  42.     foreach $tmp1(@wordDiff){
  43.               $sameNum=0;
  44.               foreach $tmp2(@wordAll){
  45.                        if ($tmp1 eq $tmp2){
  46.                           $sameNum++;
  47.                           }
  48.                        }
  49.               $diff{$tmp1}=$sameNum;
  50.               }
  51.     return %diff;

  52.     }
复制代码

偷看了下答案,shock死我了,hash真是精妙东东

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;

  4. open FILE,"c:\\zz.txt";
  5. my @words=<FILE>;
  6. my %diff;
  7. my $key;
  8. my $value;
  9. close FILE;
  10. foreach (@words){
  11.           chomp;
  12.           $diff{$_}+=1;
  13. }
  14. while (($key,$value)=each %diff){
  15.          print "The word ".$key." appeard ".$value." times\n";
  16. }
复制代码

[ 本帖最后由 godsad 于 2009-8-3 13:30 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP