I'm trying to validate that an input to my script (csv) has the column names i'm expecting. The catch is that some of the column names have special characters in them (open and closed parenthesis in this case).
I can get the code to work if I use the experimental feature "SmartMatch" but have been warned to not use it. - How can I check if a Perl array contains a particular value?
Why isn't my grep working?
my @valid_column_names = (
"some (value 1)",
"some (value 2)",
"something else",
);
my $key = "some (value 1)"; #Test Case 1
my $key = "something else"; # Test Case 2
foreach my $val (@valid_column_names){
if ( grep(/^$key$/, @valid_column_names) ){ # <--- Why won't you match!?
print "IF - key: \"$key\" val: \"$val\"\n";
} elsif ( $key ~~ @valid_column_names ){
print "ELSIF - key: \"$key\" val: \"$val\"\n";
} else {
print "ELSE - key: \"$key\" val: \"$val\"\n";
}
}
Test Case 1 output
ELSE - key: "some (value 1)" val: "some (value 1)"
ELSE - key: "some (value 1)" val: "some (value 2)"
ELSE - key: "some (value 1)" val: "something else"
Test Case 2 output
IF - key: "something else" val: "some (value 1)"
IF - key: "something else" val: "some (value 2)"
IF - key: "something else" val: "something else"