#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?
In reply to Problems with a regex? by dwlepage
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |