小编bib*_*bhu的帖子

有人能指出我在这个perl程序中所犯的错误吗?

根据下面的代码,我应该bibhu打印一次,j如果我在运行时输入"bibhu",则应该为1.但它没有发生.我为什么要来j=0

print "enter\n";
$find=<STDIN>;
$j=0;
@lines=qw(bibhu prasanna behera kuni shun jbjdkj);    
foreach (@lines) {
    if ($_ =~ /$find/) {
        print "$_\n";
        $j=$j+1;
    }
}
print "$j\n";
Run Code Online (Sandbox Code Playgroud)

regex perl

3
推荐指数
1
解决办法
91
查看次数

我正在尝试使用perl的pascals三角形.我的输出有点不同.有人可以请指出错误是什么?

我的节目是 -

@arr=('1','1  1');
print "enter the number of rows\n";
$rows=<STDIN>;
$v=0;
while($v<$rows)
{
     foreach $ele (@arr)
        {
          @arr1=split(/\s+/,$ele);
          $g=@arr1;
          #print "$g\n";
          $i=0;
          $j=1;
          $str='';
          while ($j<$g)
             {
                $res=$arr1[$i]+$arr1[$j];
                #print "$res\n";
                $i=$1+1;
                $j=$j+1;
                $str=$str.' '.$res;

            }

        $final='1'.' '.$str.' '.'1';
        print "$final\n";


}
$v=$v+1;
#@arr=();
push(@arr,$final);
}

print "@arr";

foreach (@arr)
{

    print "$_\n";
}
Run Code Online (Sandbox Code Playgroud)

我的输出是:

C:\Perl\bin>perl pascal.pl
enter the number of rows
4
1
1  1
1  2 1
1  3 3 1
1  4 6 4 1
1  5 …
Run Code Online (Sandbox Code Playgroud)

perl loops

0
推荐指数
1
解决办法
45
查看次数

确定列表是否是列表的子列表

list1=[1,3,8,10,23,8,8,10,23,3,8,10,23,3,8,10,23]
list2=[10,23,3]
cnt=list1.count(list2[0])
cnt1=1
j=0
while (cnt1<=cnt):
    list3=[]
    list3.append(list2[0])
    i=1
    k=list1.index(list2[0])
    while (i<len(list2)):
        list3.append(list1[k+i])
        i=i+1
    print (list3)    
    if (list2==list3):
        print ("list2 is a subset")
        j=j+1
    else:
        print ("list2 is not a subset")

    list1.remove(list2[0])    
    cnt1=cnt1+1    
print (list2,"occurs",j,"times")
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误.

Traceback (most recent call last):
  File "C:\Python26\Lib\idlelib\sublist.py", line 12, in <module>
    list3.append(list1[k+i])
IndexError: list index out of range"
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
42
查看次数

标签 统计

perl ×2

loops ×1

python ×1

python-3.x ×1

regex ×1