dwlepage has asked for the wisdom of the Perl Monks concerning the following question:
#Parse the Netscreen configuration open (NETSCREEN, "<net.config") || die "Cannot open file: $!\n"; my %netscreenServices = (); my @stack; foreach my $line (<NETSCREEN>) { chomp $line; if ($line =~ m/set\sservice\s(\".+\").?/) { my $serv = $1; print "Service name: $serv\n"; if (&check_stack($serv)) { print "It's on stack, keep compiling\n"; } else { print "Popping stack: @stack. Adding to stack: $1\n"; pop(@stack); push(@stack, $1); } } #print "$line\n"; } close (NETSCREEN); ###################################################################### +############# # Subfunctions ###################################################################### +############# #Checks the last element on the stack. Netscreen services are piled in +to multiple lines so we #need to keep track of the last entry. #Return TRUE if the last item is on the stack sub check_stack { my $stack = $_[0]; if (grep(/^$stack$/, @stack)) { return 1; } else { return 0; } }
When I run it I get:
Service name: "MY NEW SERVICE (TMP)"
Popping stack: . Adding to stack: "MY NEW SERVICE (TMP)"
Service name: "MY NEW SERVICE (TMP)"
Popping stack: "MY NEW SERVICE (TMP)". Adding to stack: "SGID IMAGE SERVER (TMP)"
For some reason the value is not matching in the subroutine. It seems to have something to do with the parens in the string but i'm at a loss. Does anyone have an idea?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with a regex?
by BrowserUk (Patriarch) on Aug 14, 2012 at 15:28 UTC | |
by hbm (Hermit) on Aug 14, 2012 at 15:59 UTC | |
by BrowserUk (Patriarch) on Aug 14, 2012 at 16:05 UTC | |
by hbm (Hermit) on Aug 14, 2012 at 16:38 UTC | |
by BrowserUk (Patriarch) on Aug 14, 2012 at 18:06 UTC | |
|
Re: Problems with a regex?
by AnomalousMonk (Archbishop) on Aug 14, 2012 at 16:17 UTC | |
|
Re: Problems with a regex?
by 2teez (Vicar) on Aug 14, 2012 at 16:32 UTC | |
by dwlepage (Novice) on Aug 15, 2012 at 18:22 UTC |