my $state; open SESAME, "infile"; while () { # Setting the "states" of the "state machine" if ( $_ =~ /FH/) { $state = "FH" } if ( $_ =~ /BH/) { $state = "BH"} . . # Do different things with the data depending on the # settings of the "state machine" if ( $state eq "FH" ) { # do this } if ( $state eq "BH" ) { # do that } . . } close SESAME;