... if (/^##recstart/) { my ($junk,$recno) = split; # $recno declared for this block $recno =~ s/'//g; } elsif (/^##v/) { my ($junk,$qno,$junk2,$vtext) = split (/ /,$_,4); # both $qno and $vtext declared for this block $qno =~ s/'//g; $vtext =~ s/'//g; } else { # nothing declared at all for this else block print OUT "$recno^$qno^$vtext"; } }