To verify if the variable $str contains the word “test” you would use

Options
- if ($str = ~/test/)
- if ($str == “test”)
- if ($str = “test”)
- if ($str contains “test”)


CORRECT ANSWER : if ($str = ~/test/)

Discussion Board
String variable

Your "CORRECT ANSWER" yields true for all strings $str

{ my $str = '''' ; if ($str = ~/test/) { $ret = "true"; } else { $ret = ''false''; }; print "[$ret]\n"; }
{ my $str = ''test''; if ($str = ~/test/) { $ret = "true"; } else { $ret = ''false''; }; print "[$ret]\n"; }

I would normally write
if ($str =~ /test/)


String variable 12-25-2017 11:26 PM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement