use strict; while ( ) { my @line = split /\|/, $_; my @null = grep { defined $_ } map { $line[$_] =~ /^$/ ? $_ : undef } (0..$#line); print "line contains ", scalar @null, " null fields at offset(s): @null\n"; } #line contains 3 null fields at offset(s): 1 3 4 #line contains 2 null fields at offset(s): 2 4 #line contains 3 null fields at offset(s): 0 1 4 #line contains 2 null fields at offset(s): 1 2 4 __DATA__ first||third|| alpha|beta||delta| ||c|d| one|||four|