PERL语言 $&;什么意思
1:#!/usr/local/bin/perl2:3:while($inputline=){4:while($inputline=~/\b[A-Z]\S+/g){5:$w...
1 : #!/usr/local/bin/perl
2 :
3 : while ($inputline = ) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : $wordlist{$word} += 1;
8 : }
9 : }
10: print ("Capitalized words and number of occurrences:\n");
11: foreach $capword (keys(%wordlist)) {
12: print ("$capword: $wordlist{$capword}\n");
13: }
新手求解释,而且我这段代码运行不起来的,那里错了?
最主要的是第5行, $word = $&;
中的 $& 是什么意思! 展开
2 :
3 : while ($inputline = ) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : $wordlist{$word} += 1;
8 : }
9 : }
10: print ("Capitalized words and number of occurrences:\n");
11: foreach $capword (keys(%wordlist)) {
12: print ("$capword: $wordlist{$capword}\n");
13: }
新手求解释,而且我这段代码运行不起来的,那里错了?
最主要的是第5行, $word = $&;
中的 $& 是什么意思! 展开
2个回答
展开全部
#!/usr/local/bin/perl
while ($inputline =(<>) ) { #这段。
while ($inputline =~ /\b[A-Z]\S+/g) {
$word = $&;
$word =~ s/[;.,:-]$//; # remove punctuation
$wordlist{$word} += 1;
}
}
print ("Capitalized words and number of occurrences:\n");
foreach $capword (keys(%wordlist)) {
print ("$capword: $wordlist{$capword}\n");
}
Perl一些内置常量:
是这样
$& :与格式匹配的字符串
$` : 格式匹配信息前的字符串
$’: 格式匹配信息后的字符串
例如:
$inputline =" 12 asdf Capliz qqr 124";
$inputline =~ /\b[A-Z]\S+/;
$`:"12 asdf "
$&:"Capliz"
$':" qqr 124"
while ($inputline =(<>) ) { #这段。
while ($inputline =~ /\b[A-Z]\S+/g) {
$word = $&;
$word =~ s/[;.,:-]$//; # remove punctuation
$wordlist{$word} += 1;
}
}
print ("Capitalized words and number of occurrences:\n");
foreach $capword (keys(%wordlist)) {
print ("$capword: $wordlist{$capword}\n");
}
Perl一些内置常量:
是这样
$& :与格式匹配的字符串
$` : 格式匹配信息前的字符串
$’: 格式匹配信息后的字符串
例如:
$inputline =" 12 asdf Capliz qqr 124";
$inputline =~ /\b[A-Z]\S+/;
$`:"12 asdf "
$&:"Capliz"
$':" qqr 124"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询