- 论坛徽章:
- 5
|
原帖由 MMMIX 于 2009-7-31 14:01 发表 ![]()
1. Perl 的变量名是区分大小写的, 也即 @wordALL 和 @wordAll 是不同的变量
2. 数组和标量不共享名字空间,也即 @wordDiff 和 $wordDiff 是不同的变量。
谢谢,还有好多bug 呵呵
ok了hash练习
- #!/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);
- my @tmp1=<STDIN>;
- foreach (sort(@tmp1)){
- chomp;
- print "The word ".$_." appeard ".$count{$_}." 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 ne $_){
- push @wordDiff,$_;
- }
- }
- push @wordDiff,$wordFirst;
- foreach $tmp1(@wordDiff){
- foreach $tmp2(@wordAll){
- if ($tmp1 eq $tmp2){
- $sameNum++;
- }
- }
- $diff{$tmp1}=$sameNum;
- }
- %diff;
- }
复制代码
最终版,呵呵,看来以后要多写程序了
- #!/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);
- my @tmp1=<STDIN>;
- foreach (sort(@tmp1)){
- chomp;
- print "The word ".$_." appeard ".$count{$_}." times in the file.\n";
- }
- sub diff {
- my $wordFirst;
- my @wordDiff;
- my @wordAll;
- my $sameNum;
- my $tmp1;
- my $tmp2;
- my %diff;
- my $tmp3;
- my $tmp4;
- foreach (@_){
- chomp;
- push @wordAll,$_;
- }
- $wordFirst=$wordAll[0];
- push @wordDiff,$wordFirst;
- foreach $tmp3(@wordAll){
- if ($wordFirst ne $tmp3){
- foreach $tmp4(@wordDiff){
- if ($tmp3 ne $tmp4){
- push @wordDiff,$tmp3;
- }
- }
- }
- }
- foreach $tmp1(@wordDiff){
- $sameNum=0;
- foreach $tmp2(@wordAll){
- if ($tmp1 eq $tmp2){
- $sameNum++;
- }
- }
- $diff{$tmp1}=$sameNum;
- }
- return %diff;
- }
复制代码
完美版,嗯这题涵盖了很多东东...
- #!/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);
- my @tmp1=<STDIN>;
- foreach (sort(@tmp1)){
- chomp;
- if (exists $count{$_}){
- print "The word ".$_." appeard ".$count{$_}." times in the file.\n";
- }
- else {print "The word ".$_." can`nt find in the file.\n";}
- }
- sub diff {
- my $wordFirst;
- my @wordDiff;
- my @wordAll;
- my $sameNum;
- my $tmp1;
- my $tmp2;
- my %diff;
- my $tmp3;
- my $tmp4;
- foreach (@_){
- chomp;
- push @wordAll,$_;
- }
- $wordFirst=$wordAll[0];
- push @wordDiff,$wordFirst;
- foreach $tmp3(@wordAll){
- if ($wordFirst ne $tmp3){
- foreach $tmp4(@wordDiff){
- if ($tmp3 ne $tmp4){
- push @wordDiff,$tmp3;
- }
- }
- }
- }
- foreach $tmp1(@wordDiff){
- $sameNum=0;
- foreach $tmp2(@wordAll){
- if ($tmp1 eq $tmp2){
- $sameNum++;
- }
- }
- $diff{$tmp1}=$sameNum;
- }
- return %diff;
- }
复制代码
偷看了下答案,shock死我了,hash真是精妙东东
- #!/usr/bin/perl
- use warnings;
- use strict;
- open FILE,"c:\\zz.txt";
- my @words=<FILE>;
- my %diff;
- my $key;
- my $value;
- close FILE;
- foreach (@words){
- chomp;
- $diff{$_}+=1;
- }
- while (($key,$value)=each %diff){
- print "The word ".$key." appeard ".$value." times\n";
- }
复制代码
[ 本帖最后由 godsad 于 2009-8-3 13:30 编辑 ] |
|