Perl新手求助:如何解析含子数组的数组?邮件规则修改遇阻
Hey there! Let's break down what's going wrong here and fix it step by step.
First, your current loop over @$actions is overwriting $action1 and $oldDest every time it runs. That's why when you have two actions (Mirror to + Discard), you end up with only the last one (Discard) stored in those variables—you're not handling each action individually, just clobbering the values each iteration.
What you actually want to do is process each action in the actions list, modify only the 'Mirror to' one, and keep all other actions exactly as they are. Here's how to adjust your code to do that:
use strict; use Data::Dumper qw(Dumper); my $Rules = $cli->GetAccountMailRules($account); print Dumper \@$Rules; foreach my $Rule (@$Rules) { # Only process rules with priority '1' (exact match is safer than regex) next unless $Rule->[0] eq '1'; # Cleanly unpack the rule's elements instead of assigning one by one my ($pri, $type, $auto, $actions) = @$Rule; print "\nPriority:\n", Dumper \$pri; print "\nRule Type:\n", Dumper \$type; print "\nForward Auto?:\n", Dumper \$auto; print "\nOriginal Actions:\n", Dumper \$actions; # Create a new array to hold our modified actions my @modified_actions; foreach my $action (@$actions) { my ($action_type, $dest) = @$action; if ($action_type eq 'Mirror to') { # Split the destination addresses using your specified delimiters my @addresses = split /[;,]|\\e/, $dest; # Filter to keep only addresses matching your corp domains my @dests = grep { /corp\.com|corp1\.com|corp2\.com|corp3\.com/ } @addresses; my $new_dest = join(",", @dests); # Add the modified Mirror to action to our new list push @modified_actions, [ $action_type, $new_dest ]; } else { # Keep all other actions (like Discard) exactly as they are push @modified_actions, $action; } } # Build the updated rule - reuse original values except for modified actions my $updated_rule = [ $pri, $type, $auto, \@modified_actions ]; print "\nUpdated Rule:\n", Dumper $updated_rule; # Uncomment this line to push the updated rule back to the system # $cli->UpdateAccountMailRule($account, $updated_rule); }
Key Fixes & Improvements:
- Avoided variable overwrites: Instead of storing a single action in variables, we build a new
@modified_actionsarray that preserves all actions—only modifying the 'Mirror to' entry. - Cleaner variable unpacking: Using
my ($pri, $type, $auto, $actions) = @$Rulemakes the code easier to read and maintain. - Safer priority check: Using
eq '1'ensures we only match rules with an exact priority of 1, not any rule that happens to have a '1' in its priority string. - Preserved non-target actions: Actions like 'Discard' stay untouched, so you don't lose any existing rule behavior.
- Reused original auto data: Since you're not modifying the auto settings, we just pass the original
$autoarray directly into the updated rule instead of rebuilding it manually.
When you run this with your second test case (Mirror to + Discard), the updated rule will keep the Discard action intact while modifying only the Mirror to destination list to include only your corp domains.
内容的提问来源于stack exchange,提问作者sarge

