Hi, I'm stuck on something and was hoping someone could shed some light. I'm attempting to match on a certain value stored in an array but for whatever reason it will not match. Here is the code:
#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; } }

__DATA__
set service "MY NEW SERVICE (TMP)" protocol tcp src-port 0-65535 dst-port 4103-4103
set service "MY NEW SERVICE (TMP)" + tcp src-port 0-65535 dst-port 4105-4105

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.