I have an array @test. What’s the best way to check if each element of the array is the same string?
I know I can do it with a foreach loop but is there a better way to do this? I checked out the map function but I’m not sure if that’s what I need.
If the string is known, you can use
grepin scalar context:Otherwise, use a hash:
or a shorter version:
NOTE: The
undefined value behaves like the empty string ("") when used as a string in Perl. Therefore, the checks will return true if the array contains only empty strings andundefs.Here’s a solution that takes this into account: