#! perl -slw use strict; my %charsByLine; my %freqCharsPerLine; my %chars; while( <> ) { chomp; for( split '', $_ ) { $chars{ $_ } = 1; push @{ $charsByLine{ $_ } }, $.; $freqCharsPerLine{ $. }{ $_ }++ ; } } my $last = $.; ## Eliminate chars that do not appear in every line @{ $charsByLine{ $_ } } != $last and delete $chars{ $_ } for keys %chars; ## Eliminate chars where they appear a different number of time per line for my $char ( keys %chars ) { my $previousCount = $freqCharsPerLine{ 1 }{ $char }; for my $line ( 2 .. $last ) { if( $freqCharsPerLine{ $line }{ $char } != $previousCount ) { delete $chars{ $char }; last; } } } if( keys %chars == 1 ) { print "The delimiter for this file is: ", keys %chars; } elsif( keys %chars ) { print "Candidate delimiters for this file are: ", keys %chars; } else { print "Unable to determine a likely candidate for this file!"; }