2

I'm trying to modify a vendor's perl script to evaluate whether a string exists in the current element from an array, after first evaluating whether another string exists in the same (I think) element.

I want to look for "Forward to|Redirect to|Mirror to" and if exists then look for "Discard". Right now their code looks for "Discard" whether or not the previous strings exist or not.

sub processAccount {
my $account=$_[0];
my $Rules=$cli->GetAccountMailRules($account);

  foreach my $Rule (@$Rules) {
    my $actions=$Rule->[3];

    foreach my $actn (@$actions) {
      my $a=$actn->[0];
      if($a=~/Forward to|Redirect to|Mirror to/) {
        print "$account: '$a' -> $actn->[1]\n";
      } 
      if($a=~/Discard/) {
        print "$account: Discard\n";
      }

    }
  }

I've tried nesting the if loop looking for "Discard", but it never evaluates as true.

foreach my $actn (@$actions) {
  my $a=$actn->[0];
  if($a=~/Forward to|Redirect to|Mirror to/) {
    print "$account: '$a' -> $actn->[1]\n";
    if($a=~/Discard/) {
      print "$account: Discard\n";
     }
   }
 }
Randy
  • 21
  • 1
  • 1
    That pretty clearly tells you that it's *not* found in the same element, and so you need to create a flag to carry state from one to another. – hobbs Sep 03 '19 at 21:37
  • 1
    The nested one seems all good and clear -- just not there? Note that you can combine this to `/(?:Forward to|...).*?Discard)/` (with ellipsis for the rest of that long alternation), if `Discard` indeed comes _after_ (and if you don't need to know when the first phrase is there but `Discard` isn't). Note that names `$a` and `$b` are a bit of no-no since `sort` uses them. The `$a` is declared so it's good but ... (makes me nervous to see an `$a` outside of `sort`) – zdim Sep 03 '19 at 22:12
  • It would be useful to see the data that you tested this on. Maybe it's just missing a case where `$a` matches *both* regular expressions. By the way, follow @zdim's advice about `$a`, e.g. see here https://stackoverflow.com/questions/26143243/in-perl-do-a-and-b-have-any-special-use-outside-of-the-sort-function – polettix Sep 05 '19 at 06:23

0 Answers0