perl的for循环为什么只执行了一次

本意是想统计下表中第一列各个Scaffold的重复序列类型(即第4列)的情况
##FLIE3                                 
#Scaffold1        1       6567   LTR     LTR    
#Scaffold1        4236    20683  LTR     Gypsy  
#Scaffold2        18612   20763  LTR     Gypsy  
#Scaffold3        20764   25446  LTR     Gypsy  
#Scaffold1        25447   44695  LTR     LTR    
#Scaffold3        43913   59363  LTR     Gypsy  
#Scaffold5        44696   52695  LTR     Gypsy  
#Scaffold66        57364   89631  LTR     Gypsy  
#Scaffold96        78853   92154  LTR     Gypsy
#...

use strict;
open(FILE3,"/home/test/ltt/homework1/out3.gff");


for (my $i=1;$i<=96;$i++){
        my @line = ();
        my %has = ();
        foreach(<FILE3>){

                chomp;
                @line= split /\t/;
                if ($line[0] =~ /$i/){
                        $has{$line[3]}++;
                        $has{$line[3]}.="\n";
                        }
        }
        print "The situation of chromosome$i is:\n";
        print %has;
}

运行结果如下:
[test@login01 homework1]$ perl 7.sh
The situation of chromosome1 is:
 Unknown17445
 RC88
 Simple_repeat368
 DNA4947
 Satellite261
 LINE2156
 LTR77095
The situation of chromosome2 is:
The situation of chromosome3 is:
The situation of chromosome4 is:
The situation of chromosome5 is:
The situation of chromosome6 is:
The situation of chromosome7 is:
The situation of chromosome8 is:
The situation of chromosome9 is:
The situation of chromosome10 is:
The situation of chromosome11 is:
The situation of chromosome12 is:
The situation of chromosome13 is:
The situation of chromosome14 is:
The situation of chromosome15 is:
The situation of chromosome16 is:
The situation of chromosome17 is:
The situation of chromosome18 is:
The situation of chromosome19 is:
The situation of chromosome20 is:
The situation of chromosome21 is:
The situation of chromosome22 is:
The situation of chromosome23 is:
The situation of chromosome24 is:
The situation of chromosome25 is:
The situation of chromosome26 is:
The situation of chromosome27 is:
The situation of chromosome28 is:
The situation of chromosome29 is:
The situation of chromosome30 is:
The situation of chromosome31 is:
The situation of chromosome32 is:
The situation of chromosome33 is:
The situation of chromosome34 is:
The situation of chromosome35 is:
The situation of chromosome36 is:
The situation of chromosome37 is:
The situation of chromosome38 is:
The situation of chromosome39 is:
The situation of chromosome40 is:
The situation of chromosome41 is:
The situation of chromosome42 is:
The situation of chromosome43 is:
The situation of chromosome44 is:
The situation of chromosome45 is:
The situation of chromosome46 is:
The situation of chromosome47 is:
The situation of chromosome48 is:
The situation of chromosome49 is:
The situation of chromosome50 is:
The situation of chromosome51 is:
The situation of chromosome52 is:
The situation of chromosome53 is:
The situation of chromosome54 is:
The situation of chromosome55 is:
The situation of chromosome56 is:
The situation of chromosome57 is:
The situation of chromosome58 is:
The situation of chromosome59 is:
The situation of chromosome60 is:
The situation of chromosome61 is:
The situation of chromosome62 is:
The situation of chromosome63 is:
The situation of chromosome64 is:
The situation of chromosome65 is:
The situation of chromosome66 is:
The situation of chromosome67 is:
The situation of chromosome68 is:
The situation of chromosome69 is:
The situation of chromosome70 is:
The situation of chromosome71 is:
The situation of chromosome72 is:
The situation of chromosome73 is:
The situation of chromosome74 is:
The situation of chromosome75 is:
The situation of chromosome76 is:
The situation of chromosome77 is:
The situation of chromosome78 is:
The situation of chromosome79 is:
The situation of chromosome80 is:
The situation of chromosome81 is:
The situation of chromosome82 is:
The situation of chromosome83 is:
The situation of chromosome84 is:
The situation of chromosome85 is:
The situation of chromosome86 is:
The situation of chromosome87 is:
The situation of chromosome88 is:
The situation of chromosome89 is:
The situation of chromosome90 is:
The situation of chromosome91 is:
The situation of chromosome92 is:
The situation of chromosome93 is:
The situation of chromosome94 is:
The situation of chromosome95 is:
The situation of chromosome96 is:





![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/636156642626191.PNG)


img

估计if ($line[0] =~ /$i/)这个条件只有第一次时候是满足的。

可能是因为foreach循环中控制变量的值会被Perl自动保存和恢复。当循环进行时,是没有办法改变其值的。循环结束时,变量的值会回到循环开始前,如果没有值则为undef。这意味着如果有一个变量和控制变量有相同的名字。如果在循环中修改这个变量,那原始列表中的元素也会被修改
你可以看看https://www.cnblogs.com/chip/p/4188557.html