[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] /AAA|BBB/




Huh. I'm still learning stuff. :)

(1) Here's a quiz for you. What does this print?:

foreach (qw(AABBB AAABB AAA BBB AAB AA)) {
  print "$_ ";
  print (($_ =~ /AAA|BBB/) ? "yes" : "no");
  print "\n";
}

(2) What does this print (what does adding parens change)?:

foreach (qw(AABBB AAABB AAA BBB AAB AA)) {
  print "$_ ";
  print (($_ =~ /(AAA|BBB)/) ? "yes" : "no");
  print "\n";
}

(3) What does this print?

foreach (qw(AABBB AAABB AAA BBB AAB AA)) {
  print "$_ ";
  print (($_ =~ /^(AAA|BBB)$/) ? "yes" : "no");
  print "\n";
}

When I'm looking for an exact match from a list of possibles I've always used syntax 3. I was surprised to see syntax 1 the other day, and didn't think it was doing what the author intended. I was partially right...

I guess this kind of stuff is a strong argument for 'use Switch;' -grin-

j



(1)
AABBB yes
AAABB yes
AAA yes
BBB yes
AAB no
AA no

(2)
AABBB yes
AAABB yes
AAA yes
BBB yes
AAB no
AA no

(3)
AABBB no
AAABB no
AAA yes
BBB yes
AAB no
AA no