The key problem lies in your lack of understanding in what data structure is required. In your design example, you assigned 6 values to 2 variables. That doesn't add up.

You could have an array of port channel records:

$port_channels[0]{ name => "port-channel 1" , members => [ 'fc2/5' , 'fc1/5' ] }; $port_channels[1]{ name => "port-channel 3" , members => [ 'fc1/1' ] };

But why not just index by the port-channel:

$port_channels{1}{ members => [ 'fc2/5' , 'fc1/5' ] }; $port_channels{3}{ members => [ 'fc1/1' ] };

Since your record only has one field, why not forget the record and just have an array:

$port_channels{1}[ 'fc2/5' , 'fc1/5' ]; $port_channels{3}[ 'fc1/1' ];

The following implements the last one. It creates a hash of port channels, where a port channel is an array of ports.

use strict; use warnings; my %port_channels; { my $channel; my $in_ports = 0; while (<DATA>) { if (/^port-channel\s+(\d+)/) { $channel = $1; $port_channels{$channel} = []; $in_ports = 0; next; } next unless defined $channel; if (/^\s*Ports:\s+(\S+)\s+\[/) { $in_ports = 1; push(@{$port_channels{$channel}}, $1); next; } if ($in_ports) { if (/\s+(\S+)\s+\[/) { push(@{$port_channels{$channel}}, $1); next; } $in_ports = 0; } } } require Data::Dumper; print(Data::Dumper::Dumper(\%port_channels)); __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', 'fc1/5' ], '3' => [ 'fc1/1' ] };

Update: Added the explanation at the top.


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