in reply to help with loop

Very very very similar question Re: Help with locating bp region in chromosome/Re^5: Help with locating bp region in chromosome.

You might write as

#!/usr/bin/perl -- use strict; use warnings; use autodie; use Getopt::Long qw/ GetOptionsFromArray /; use Pod::Usage; use Regex::PreSuf (); Main( @ARGV ); exit( 0 ); sub Main { GetOptionsFromArray( \@_, 'list=s' => \my $inlist, 'pos=s' => \my $inpos, 'out=s' => \my $outpos, help => sub { pod2usage(-verbose => 2); }, ) or return pod2usage(2); return pod2usage(2) unless $inlist and $inpos and $outpos; my $list_re ; { open my($listfh), '<', $inlist; $list_re = Regex::PreSuf::presuf( map { chomp; $_ } readline( +$listfh ) ); close $listfh; } open my($posfh), '<', $inpos; open my($outfh), '>', $outpos; while( <$posfh> ){ my( $geneId, $SNPpos ) = ( split /\t/, $_ )[ 0, 2]; if( $geneId =~ /$list_re/ ){ print $outfh join "\t", $geneId, $SNPpos, "\n"; } } close $posfh; close $outfh; } =head1 NAME HI - hi =head1 SYNOPSIS shaba -l foo.txt -p pos.txt -o out.txt shaba -l=foo.txt -p=pos.txt -o=out.txt shaba --help =head1 ARGUMENS =over 4 =item C<-list> -list =item C<-pos> -pos =item C<-out> -out =back =cut

See also Tutorials: Variable Scoping in Perl: the basics, Coping with Scoping )

Also, Use Getopt::Long even if you don't think you need to - Mechanix