Your regex is complicated and likely to be buggy. The use of greedy .* may blow up in your face.

What is @line, and what is its relation to $line?

I'd get at the match variable problem like this:

if ($line[0] =~ /^c/) { # . . . my $unit = ($line =~ /\bunit=(\d*)/) ? $1 : ''; my $port = ($line =~ /\bport=(\d*)/) ? $1 : ''; # . . . }
With that, the $unit and $port variables can only be assigned from $1 if the match succeeded and the value fresh. If the match fails the empty string is assigned, which will save warnings about undefined values when you print.

After Compline,
Zaxo


In reply to Re: matching patterns by Zaxo
in thread matching patterns by theroninwins

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.