use Modern::Perl;
my @same = (8) x 8; # an array of eight 8s
my @notSame = qw/ 10 10 10 12 10 10 /;
say "The array's elements are"
. ( sameArrayElements(@same) ? '' : ' not' )
. ' the same.';
sub sameArrayElements {
my %hash = map { $_ => 1 } @_;
keys %hash == 1 ? 1 : 0;
}
If you send the subroutine sameArrayElements an array, it'll return 1 (true) if the array's elements are identical, and 0 (false) otherwise (even if an empty array is sent). As shown above, the output is:
The array's elements are the same.
Try sameArrayElements with @notSame.
The subroutine uses map to iterate through all elements of the sent array, populating %hash with key/value pairs (the $_ => 1 notation). If the array elements are identical, there should be only one key--and the last line in the subroutine tests for that.
Thus, if you pushed each numLines from a dir read onto an array, and then sent that array to sameArrayElements, it'll tell you whether all files in that dir have the same number of lines or not. (Be sure the array's empty, e.g, by using my @array;, before pushing values onto it.) |