The following snippet works fine, with local $/ = undef and (string=~//g){$count++} syntax gleaned from Catagorized Questions and Answers.   It's the output end of a script which uses UCD-SNMP to check a passel o' Ethernet switches for port occupancy.  

Here's my question - can repetition of lines 16-19 be re-written more concisely?  
    cheers,
    Don
    striving for Perl Adept

1 #!/usr/bin/perl -w 2 3 use strict; 4 5 my $infile = './count.in'; 6 my $vlan = "0"; 7 my $up = "0"; 8 my $down = "0"; 9 my $shut = "0"; 10 11 { 12 local $/ = undef; 13 open(IN, "<$infile") or die "Cannot open $infile: $!"; 14 my $filestring = <IN>; 15 close IN or die "Cannot close $infile: $!"; 16 while ($filestring =~ /VLAN/g) { $vlan++ } 17 while ($filestring =~ /ifOperStatus.*up\(1\)/g) { $up++ } 18 while ($filestring =~ /ifOperStatus.*down\(2\)/g) { $down++ } 19 while ($filestring =~ /ifAdminStatus.*down\(2\)/g) { $shut++ } 20 } 21 22 my $live = ($up - $vlan); 23 my $total = ($live + $down); 24 25 print "$vlan VLAN(s)\n"; 26 print "$total physical ports\n"; 27 print "$live active port(s)\n"; 28 print "$down inactive port(s)\n"; 29 print "$shut disabled port(s)\n";
Sample Input (./count.in)
interfaces.ifTable.ifEntry.ifAdminStatus.1 = up(1) interfaces.ifTable.ifEntry.ifAdminStatus.2 = up(1) interfaces.ifTable.ifEntry.ifAdminStatus.3 = up(1) interfaces.ifTable.ifEntry.ifAdminStatus.4 = up(1) interfaces.ifTable.ifEntry.ifAdminStatus.5 = down(2) interfaces.ifTable.ifEntry.ifOperStatus.1 = up(1) interfaces.ifTable.ifEntry.ifOperStatus.2 = up(1) interfaces.ifTable.ifEntry.ifOperStatus.3 = up(1) interfaces.ifTable.ifEntry.ifOperStatus.4 = down(2) interfaces.ifTable.ifEntry.ifOperStatus.5 = down(2) interfaces.ifTable.ifEntry.ifDescr.1 = VLAN1 interfaces.ifTable.ifEntry.ifDescr.2 = FastEthernet0/1 interfaces.ifTable.ifEntry.ifDescr.3 = FastEthernet0/2 interfaces.ifTable.ifEntry.ifDescr.4 = FastEthernet0/3 interfaces.ifTable.ifEntry.ifDescr.5 = FastEthernet0/4

In reply to Cut n' paste monkey on my back by ybiC

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.