Thanks again. I don't think I'm fully grasping how vec works, even after reading the doc, or the routine using vec to map the bits to the vlans and creating the ranges.

The routine formating numbers like 1,2,3 as 1-3 is great, but doesn't seem to work properly with the octet string example. Of course it won't work with Grandfather's example from what I can see since @ports just contains the VLANs allowed

Trying to shoehorn the previous example into this one:

sub xlat { my ($octets) = @_ ; return pack('B*', unpack('b*', $octets)) ; } ; my $test = '0x4000040000000200800000000000000000000002' .'0000000000c004000108008000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000'; my $bits; $bits = showbits($test); my @s = ranges($bits); print "Ranges: ", join(', ', @s); $bits = showbits(xlat($test)); @s = ranges($bits); print join(',',@s); $bits = showbits(xlat(xlat($test))); @s = ranges($bits); print join(',',@s); sub ranges{ my $test = shift; my $r = undef ; my @s = () ; for my $vn (1..length($test) * 8) { if (vec($test, $vn, 1)) { if (!defined($r)) { push @s, "$vn-" ; } ; $r = $vn ; } else { if (defined($r)) { $s[-1] .= "$r" ; $r = undef ; } ; } ; } ; return @s; } sub showbits { my ($octets) = @_ ; my $s = unpack('B*', $octets) ; $s =~ s/([01]{8})(?=[01])/$1/g ; ranges($s) ; } ;

Should, at least in my misunderstanding, produce Ranges: 1,21,54,64,158,200-201,213,231,236,248.

Instead it produces:

Ranges: 1-1, 4-5, 8-8, 10-10, 12-13, 18-18, 20-21, 24-24, 28-291-1,4-5,8-8,10-10,12-13,18-18,20-21,28-291-1,4-5,8-8,10-10,12-13,18-18,20-21,24-24,28-29

Using GrandFather's example

my $ports = '0x4000040000000200800000000000000000000002' .'0000000000c004000108008000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000'; my $basePort = 0; my @ports; $ports = substr $ports, 2; while ($ports) { my $octet = hex substr $ports, 0, 2, ''; my $index = 0; while ($octet) { next unless $octet & 0x80; push @ports, $basePort + $index; } continue { ++$index; $octet = ($octet << 1) & 0xff; } $basePort += 8; } print join ',', @ports;

produces: 1,21,54,64,158,200,201,213,231,236,248

After hours of playing with both these examples I managed to confuse myself even more. Can you point out what I'm missing?

Update:After some more tinkering I came up with the following kludge to format Grandfather's example, but something tells me there's a better way to do this. Still haven't figured out how to convert the string properly with vec.

my $ports = '0x4000040000000200800000000000000000000002' .'0000000000c004000108008000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000000000000000000000000000000000' .'00000000000'; my $basePort = 0; my @ports; $ports = substr $ports, 2; while ($ports) { my $octet = hex substr $ports, 0, 2, ''; my $index = 0; while ($octet) { next unless $octet & 0x80; push @ports, $basePort + $index; } continue { ++$index; $octet = ($octet << 1) & 0xff; } $basePort += 8; } print join ',', @ports,"\n"; range_format(@ports); sub range_format{ my (@numbers,$i,$start,$end); @numbers = @_; for ($i = 0; $i < @numbers; ++$i){ my $prev = $numbers[$i-1]; my $next = $numbers[$i+1]; my $cur = $numbers[$i]; if (($cur+1 == $next) and ($cur-1!= $prev)){ $start = $cur; } if (($cur-1 == $prev) and ($cur+1 != $next)){ $end = $cur; } if ((defined $start) and (defined $end)){ print "$start-$end,"; $start = undef; $end = undef; }else{ unless((defined $start)||(defined $end)){print "$cur,";}} } }

In reply to Re^4: Allowed VLANs with SNMP by spivey49
in thread Allowed VLANs with SNMP by spivey49

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.