关于perl

niepengli

普通会员
本人最近在自学perl,可是发现自己编写的代码1)在执行时毫无反应,不知何故?还有,有些例子来自于Practical Text Mining with perl,或Hammond的书照搬到自己的电脑,如2)执行时错误颇多,不知是版本冲突问题,还是其他问题,用activeperl的critic编译,各种问题都有,哪位专家能帮忙解释一下吗?
1)
[highlight=perl]
my @text=("university, Carleton’s faculty and staff provide a superior learning experience for our fine students who hail from every province and from over 100 countries around the world. Carleton offers 65 programs of study in areas ");

@word = (split /[ \.\?,!;:]+/,@text);
@word=split/ /,@text;

foreach $word (sort @word) {
$word = lc $word;
}

foreach my $word (keys my %words){
print %word ;

}
[/highlight]

2)
[highlight=perl]
open F, $ARGV[0] or die "Enter a file name\n";

$initial = 1;

while ($line = <F>) {
chomp $line;
@words = split /([ \.\?,!;:]+)/, $line;
foreach $word (@words) {
if ($word =~ /[\.\?!]+/) {
$initial = 1;
} elsif ($word =~ /[ ;:]+/) {
#do nothing in this case!
} elsif ($initial) {
$initial = 0;
if (exists $init{$word}) {
$init{$word}++;
} else {
$init{$word} = 1;
}
} else {
if (exists $medial{$word}) {
$medial{$word}++;
} else {
$medial{$word} = 1;
}
}
}
}

close F;


foreach $initword (keys %init) {
$lcword = $initword;
$lcword =~ tr/A-Z/a-z/;
if (exists $medial{$initword}) {
$medial{$initword} += $init{$initword};
} elsif (exists $medial{$lcword}) {
$medial{$lcword} += $init{$initword};
} else {
$medial{$initword} = $init{$initword};
}
}


foreach $word (sort {lc($a) cmp lc($b)} keys %medial) {
print "$word:\t$medial{$word}\n";
}
[/highlight]
 
Last edited by a moderator:
回复: 关于perl

Added syntax highlighting and indention for your code, making it more readable
and easier to debug.
 
回复: 关于perl

Debugging takes time. Try to print the values of variables along the way.
On another note, the program looks fairly complex and is probably not a
good way to get started. Try some simple exercises first.

Perl has a peculiar syntax. I started with Perl, but later switched to Python.
It's much cleaner and easier to understand.
 
Back
顶部