Hi,

A quick try:

Note that I check for blank lines as a seperator between 'records'. I don't do anything with the seperator yet, but, just in case... It's there if you need it.

I also place a next in my if statements, no need to keep on checking a record when the treatment has been done.

I used a hash in a hash because a) I noticed the UP besides each 'port'. So you might want to replace 'def' with the value UP whenever that UP can be something else and b) it allows you to easily check with the defined keyword for ports that were already attributed for a channel

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = {}; my $channel = undef; while (defined (my $record = <DATA>)) { chomp $record; if ( $record =~ /^$/ ) { $channel = undef; # port-channels are seperated by blank lines + next; } # I know I'm not in a record when I look for a port-channel # I could add a die here to signal problems with the format if ( !defined channel && $record =~ /^port-channel (\d+)$/ ) { $channel = $1; # this signals a new channel next; } # I check if I'm in a record and if so I test for fc's if ( $defined $channel && $record =~ /fc(\d+)\/(\d+)\s+\[(.+)\]/ ) + { my $port = "fc$1\/$2"; my $status = $3; die "something wrong" if defined $data->{$channel}->{$port}; $data->{$channel}->{$port} = $status; next; } } print Dumper $data; __DATA__ port-channel 1 Administrative channel mode is on Operational channel mode is on Last membership update succeeded First operational port is fc1/5 2 ports in total, 2 ports up Ports: fc2/5 [up] fc1/5 [up] * port-channel 3 Administrative channel mode is on Operational channel mode is on Last membership update succeeded First operational port is fc1/1 1 port in total, 1 port up Ports: fc1/1 [up] *

Output:

$VAR1 = { '1' => { 'fc2/5' => 'up', 'fc1/5' => 'up' }, '3' => { 'fc1/1' => 'up' } };

ps: this node contains interesting toughts about treating records.

--
if ( 1 ) { $postman->ring() for (1..2); }

In reply to Re: Splitting data into chunks! by gargle
in thread Splitting data into chunks! 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.