foreach $record (@contents) { # where does @contents come from? # you said a flat-file: why did you read the whole # file into an array, if you did? is that neccessary? # you'll probably rather like while(defined(my $record = <$flatfilehandle>)){ @fields = split(/\|/,$record); # @fields is global? probably not my @fields = split... $tidm=$fields[0];$tidm=~s/\s+$//g; $nice=$fields[1];$nice=~s/\s+$//g; #it's much easier to say my ($tidm,$nice) = split /\|/,$record; # and it's more effective to drop the /g, for it's # completely useless: You cannot mach the EOString more # than one time. Correct me if these variables might # contain multiple lines, but i'm "quite sure" they don't # :) $s = substr($nice,0,1);