First, are you sure your @dir array really has blank lines in it? Is it possible that some of your strings have newlines on the end of them? Try using
Data::Dumper to get a full view of the actual contents of your array. If the problem is in fact trailing newlines, then you might want to use
chomp when inserting the strings into your array.