in reply to Making two loops into one

I think your basic question is solved, and ysth has warned you about a possible risk with your open() statements. I have a slightly different admonishment about getting the file names (and min/max params):

Use @ARGV, and get the file names, and min/max values as well, from the command line, instead of forcing the user into a dialog with printed prompts and keyboard input via STDIN. You can look at the Getopt::Long and Getopt::Std modules to see how most people do this sort of thing, or you can easily "roll your own" -- something like this:

my $Usage = "Usage: $0 --min MIN --max MAX --in INFILE --out OUTFILE\n +"; my %args; while ( @ARGV >= 2 ) { if ( $ARGV[0] =~ /^--(min|max|in|out)$/ ) { my $opt = substr( shift, 2 ); $args{$opt} = shift; } else { last; } } die $Usage if ( @ARGV or ( keys %args != 4 )); # all 4 args are mandatory, and only these 4 args are allowed # now use $args{in} instead of $input, etc...
(But really, the Getopt modules are worthwhile -- reading their man pages is time well spent.)

There are several reasons for using command-line args in @ARGV instead of a runtime dialog with the user:

Note that the ordering of arg flags is flexible: "--min 0" can come before or after "--max 10", and likewise for the file name args; all that matters is that each value comes immediately after its flag.