in reply to Splitting data into chunks!

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); }

Replies are listed 'Best First'.
Re^2: Splitting data into chunks!
by blackadder (Hermit) on Sep 21, 2005 at 07:30 UTC
    WOW.....Thanks guys.

    Viva PerlMonks

    Blackadder