这个程序很简单,查找一个小字符串在大串里面出现的所有位置。
#!usr/local/bin/perl -w
use strict;
use 5.010;
my $big="This is true";
my $small="is";
my @where;
my $wh=0;
while($wh!=-1)
{
push @where, $wh;
$wh=index(substr($big,$wh),$small);
}
shift @where;#get rid of 0!
if(@where)
{say "Found $small in $big at position(s):".@where;}
else
{say "Nowhere found!";}
运行之后电脑死机,过后显示“out of memory"。
这个perl小程序为什么耗尽内存??
答案:2 悬赏:50 手机版
解决时间 2021-02-17 20:50
- 提问者网友:一抹荒凉废墟
- 2021-02-17 11:51
最佳答案
- 五星知识达人网友:野味小生
- 2021-02-17 13:29
把你的程序稍作修改你就知道为什么了
#!perl -w
use strict;
my $big="This is true";
my $small="is";
my @where;
my $wh=0;
my $n = 10;
while($n--)
{
push @where, $wh;
my $sub = substr($big,$wh);
print "$n: substr:$sub";
$wh=index($sub, $small);
print "\tindex: $wh\n";
}
shift @where;#get rid of 0!
if(@where) {
print "Found $small in $big at position(s): @where\n";
} else {
print "Nowhere found!\n";
}
这是打印的结果:
9: substr:This is true index: 2
8: substr:is is true index: 0
7: substr:This is true index: 2
6: substr:is is true index: 0
5: substr:This is true index: 2
4: substr:is is true index: 0
3: substr:This is true index: 2
2: substr:is is true index: 0
1: substr:This is true index: 2
0: substr:is is true index: 0
Found is in This is true at position(s): 2 0 2 0 2 0 2 0 2
第一次在$big中substr返回整个$big,找到"is"的位置为2,此时$wh=2;
第二次substr得到"is is true",找到"is"的位置为0,此时$wh=2;
第三次,问题出现了,substr操作的字符串是$big,$wh为0,所以substr操作返回整个big,找到"is"的位置为2.....
如此循环往复,陷入死循环,当然会内存溢出啦~~
简单的修改下你的程序就ok了
#!perl -w
use strict;
my $big="This is true";
my $small="is";
my @where;
my $wh = 0;
while(1)
{
$wh = index($big, $small, $wh);
last if $wh==-1;
push @where, $wh;
$wh++ ;
}
if(@where) {
print "Found $small in $big at position(s): @where\n";
} else {
print "Nowhere found!\n";
}
#!perl -w
use strict;
my $big="This is true";
my $small="is";
my @where;
my $wh=0;
my $n = 10;
while($n--)
{
push @where, $wh;
my $sub = substr($big,$wh);
print "$n: substr:$sub";
$wh=index($sub, $small);
print "\tindex: $wh\n";
}
shift @where;#get rid of 0!
if(@where) {
print "Found $small in $big at position(s): @where\n";
} else {
print "Nowhere found!\n";
}
这是打印的结果:
9: substr:This is true index: 2
8: substr:is is true index: 0
7: substr:This is true index: 2
6: substr:is is true index: 0
5: substr:This is true index: 2
4: substr:is is true index: 0
3: substr:This is true index: 2
2: substr:is is true index: 0
1: substr:This is true index: 2
0: substr:is is true index: 0
Found is in This is true at position(s): 2 0 2 0 2 0 2 0 2
第一次在$big中substr返回整个$big,找到"is"的位置为2,此时$wh=2;
第二次substr得到"is is true",找到"is"的位置为0,此时$wh=2;
第三次,问题出现了,substr操作的字符串是$big,$wh为0,所以substr操作返回整个big,找到"is"的位置为2.....
如此循环往复,陷入死循环,当然会内存溢出啦~~
简单的修改下你的程序就ok了
#!perl -w
use strict;
my $big="This is true";
my $small="is";
my @where;
my $wh = 0;
while(1)
{
$wh = index($big, $small, $wh);
last if $wh==-1;
push @where, $wh;
$wh++ ;
}
if(@where) {
print "Found $small in $big at position(s): @where\n";
} else {
print "Nowhere found!\n";
}
全部回答
- 1楼网友:往事隔山水
- 2021-02-17 14:01
没看懂什么意思?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯