in reply to elsif greping

Posting only on the issue you stated, it looks like you've got some syntax errors regarding parens. Untested:

for my $object (@objects) { if (grep { $_ !~ /$object/ } @config){ ... } elsif (grep { /^PPS/ } @objects){ print $fp "object-group service $object \n"; } else { print $fp "object-group network $object \n"; } }

elsif requires a condition within the traditional brackets () to examine. eg: elsif (1 == 1){ ... }. You should always have use warnings; and use strict; at the top of your scripts. In this case, you may have, but you didn't show the actual warn/error message, so it's hard to tell.

Hopefully this is enough to get you going. If not, please post back with the literal warnings/errors you received.

-stevieb

Replies are listed 'Best First'.
Re^2: elsif greping
by H0tc@xe (Initiate) on Sep 26, 2015 at 01:43 UTC
    Hi Steve, thanks for the tips. I rewrote it with a nested if inside the first one. This is the full script - gotta thank AnonMonk for the glob portion too :).
    #!/usr/bin/perl -w # use warnings; use strict; my @files = glob "Configs/*"; foreach my $file (@files) { # Read config from .txt files in Dir. open my $fh, $file or die "Can't open '$file': $!"; my @config = <$fh>; close $fh; chomp @config; my $configref = \@config; open (my $fp, '>', "$file.UNUSED"); # Extract names from config. my @names = map { /^name \d+\.\d+\.\d+\.\d+ ([A-Za-z0-9-_]+)$/ + ? $1 : () } @{ $configref }; # Remove names from config. @config = grep { $_ !~ /^name / } @config; # Find unused name references. foreach my $name (@names) { if (grep { $_ !~ /$name/ } @config) { print $fp "name $name \n"; } } # Extract objects from config. my @objects = map { /^(object|object-group) (network|service) +([A-Za-z0-9-_]+)$/ ? $3 : () } @{ $configref }; # Remove objects from config. @config = grep { $_ !~ /^(object|object-group) / } @config; # Find unused object references. foreach my $object (@objects) { if (grep { $_ !~ /$object/ } @config) { if (grep { /^PPS/ } @objects) { print $fp "object-group service $object \n"; } } else { print $fp "object-group network $object \n"; } } }

    If I take out;

    if (grep { /^PPS/ } @objects) { print $fp "object-group service $object \n"; } } else {
    and just leave in
    print $fp "object-group network $object \n";
    at the end, the whole script works - outputs unused names with names and unused obj-grps with groups. With the nested if statement included I get output of the opposite (used names/obj-grps) plus all the output is pre-pended with the string "name" instead of a mixture of "name" and "object-group network".

      :) you should take the other tip too, make subs , also autodie error checking