H0tc@xe has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I'm nearly finished and need assistance on this last bit. This part of the script is used to find unused object-groups in a config file. I'm trying to find the object-groups that have "PPS" in the name and have them output to my file with "object-group service" and all the rest output with "object-group network". STDOUT is telling me I have a syntax error at the "elsif" line and that's it. My grep syntax is probably lacking and no matter how many times I try different sets of () or "" no joy.
# 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) { elsif grep { /^PPS/ } @objects; { print $fp "object-group service $object \n"; else print $fp "object-group network $object \n"; } } }

Replies are listed 'Best First'.
Re: elsif greping
by stevieb (Canon) on Sep 26, 2015 at 00:15 UTC

    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

      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
Re: elsif greping ( perlintro, perlsyn )
by Anonymous Monk on Sep 26, 2015 at 00:44 UTC

      Nice. Point taken that I should include direct references to documentation where possible. AnonyMonk posts have become high-quality in the last while.