my $state=0; #state 0 you expect a card id or comments while ( my $line = <$FILE> ) { if ($state==0) { if ($line=~/.../) { dosemthing; $state=1; } elsif ($line=~/.../) { dosemthingelse; $state=4; else { print "error in datafile"; $state=0; } } elsif ($state==1) { if ($line=~/.../) { ...