in reply to Global variables question

See post by jwkrahn, in addition, I think your grep may not be doing what you think.
opendir( DIR1, $dir1 ) or die("Error opening: $dir1"); my @files1 = sort grep {-f "$dir1/$_"} readdir(DIR1);
may work better for you. This will filter out any directories. Also, I don't think that the files returned by readdir are guaranteed to be in any order, so if you sort, that will get things into a predictable, repeatable order.