in reply to if else statement check an array

Until good indentation becomes second nature, get a copy of Perl Tidy and use it, especially before you post code here. The structure of well indented code is much easier to understand and edit correctly.

In a subtle fashion you have nested search loops. That is always a red flag, although it is sometimes unavoidable. In this case the creation of the match string for index is executed each time through your loop - that is the subtle nested 'loop'. The general fix to such nested loops is to use a hash lookup if you can in place of one of them. The following implements that in the contxt of your problem:

use strict; use warnings; my @issued_commands = qw(that or the other); my @showconfig = qw(this that and the other); my %hits = map {$_ => 0} @showconfig; my @hit; my @extra; for my $issued_CM_command (@issued_commands) { if (exists $hits{$issued_CM_command}) { ++$hits{$issued_CM_command}; push @hit, $issued_CM_command; } else { push @extra, $issued_CM_command; } } my @missed = grep {! $hits{$_}} keys %hits; print "No entry in \@issued_commands for: @missed\n" if @missed; print "No entry in \@showconfig for: @extra\n" if @extra;

Prints:

No entry in @issued_commands for: and this No entry in @showconfig for: or

Note that there is probably a lot of magic going on in there that is a little beyond where you are at with Perl at present. That is by intent! Please read the perldoc's for any Perl functions you aren't familiar with. If you still can't come to grips with it, please ask!


True laziness is hard work