CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Nested If logic help

 



H0tc@xe
New User

Sep 26, 2015, 6:09 PM

Post #1 of 4 (985 views)
Nested If logic help Can't Post

Hello, this script should take in networking configs, extract the object-group names and find all the ones that are not used in the config. So far I have that working, but I want to make the script look at the object-group name and if it has the string "PPS" in the name then output it with 'object-group service' pre-pended to the name. Else output the match with
'object-group network'. I'm at a stand still with my logic for the bottom nested if statements. They appear correct to me?

My current results:

Code
no name aihh  
no name ai00
no object-group network Remote
no object-group network GATEY_PPS
no object-group network EXT_USRS
.
etc


My intended results:

Code
... 
no object-group network Remote
no object-group service GATEY_PPS
no object-group network EXT_USRS



Code
use warnings; 
use strict;
use autodie;

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 file and retain config name.
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 "no 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 "no object-group service $object \n";
}

else {
print $fp "no object-group network $object \n";
}
}

}
}




(This post was edited by H0tc@xe on Sep 26, 2015, 6:42 PM)


BillKSmith
Veteran

Sep 27, 2015, 8:07 AM

Post #2 of 4 (957 views)
Re: [H0tc@xe] Nested If logic help [In reply to] Can't Post


Code
if (grep { /^PPS/ } @objects) {


I see two problems with this statement:

  • It tests all the objects every time. It should only test the current object.


  • It looks for PPS at the beginning of the string. In your example, it is at the end.


  • If I understand your requirements correctly, this should be:

    Code
    if ($object =~ /PPS/) {

    Good Luck,
    Bill


    FishMonger
    Veteran / Moderator

    Sep 27, 2015, 9:28 AM

    Post #3 of 4 (947 views)
    Re: [H0tc@xe] Nested If logic help [In reply to] Can't Post

    You seem to be looping over the contents of each file too many times.

    Are the config files in a standard format?

    Can you post a sample file so that we can see if we can find a better, more efficient, way to parse it?


    H0tc@xe
    New User

    Sep 27, 2015, 10:58 AM

    Post #4 of 4 (940 views)
    Re: [BillKSmith] Nested If logic help [In reply to] Can't Post

    Bill, was right on the money. I should have been testing the scalar and not the array, plus my regex was jacked up. But tested and all is well. :) Thanks guys.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives