. means "any one character".
\. means "one period".
You keep using the wrong one.

You used (XXX)+ where you mean (XXX+).

Then there's using $2 when the regexp only has one capture.

You didn't create any records for interfaces with no descriptions (but your desired output indicates you want this).

$Recs is a more accurate name for $Rec.

The XXX, XXX if XXX syntax is unusual and therefore harder to read.

Recs has no reason for being a hash reference. A hash is sufficient.

Your indenting is inconsistent. Pick a style and stick with it.

use warnings was missing. It would have identified your problems here.

use strict; use warnings; my %Recs; my $pc_name; while (<DATA>) { if (/interface (\S+)$/) { $pc_name = $1; $Recs{$pc_name} = {}; # Create record. next; } if (/switchport description (.*)$/) { $Recs{$pc_name}{description} = $1; next; } } for $pc_name (keys %Recs) { my $description = $Recs{$pc_name}{description}; if (defined $description) { $description = "switchport description $description"; } else { $description = ""; } print("interface $pc_name = switchport description $description\n" +); }
or if you're not planning on capturing more than just the description:
use strict; use warnings; my %descriptions; my $pc_name; while (<DATA>) { if (/interface (\S+)$/) { $pc_name = $1; $descriptions{$pc_name} = undef; next; } if (/switchport description (.*)$/) { $descriptions{$pc_name} = $1; next; } } for $pc_name (keys %descriptions) { my $description = $descriptions{$pc_name}; if (defined $description) { $description = "switchport description $description"; } else { $description = ""; } print("interface $pc_name = switchport description $description\n" +); }

Update: Added missing paren.

Update: Added creation of records with no description.


In reply to Re: RegEx Blues by ikegami
in thread RegEx Blues by blackadder

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.