in reply to Splitting data into chunks!

Yet another solution...

Update1: Slightly modified to get rid of the useless "port_member" key.
Update2: Golfed, just for fun.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper::Simple; my ($Rec,$pc_name); while (<DATA>) { $pc_name = $1, next if (/(port-channel \d+)$/); $Rec->{$pc_name}->{$1} = $2 if (m|(fc\d+/\d+)\s+\[(\w+)\]|); } print Dumper($Rec); __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:
$Rec = { 'port-channel 3' => { 'fc1/1' => 'up' }, 'port-channel 1' => { 'fc1/5' => 'up', 'fc2/5' => 'up' } };

Replies are listed 'Best First'.
Re^2: Splitting data into chunks!
by blackadder (Hermit) on Sep 21, 2005 at 10:43 UTC
    Hi

    I ma trying to include this code within my script but it seems to through errors!

    My Code
    #! c:/perl/bin/perl.exe # use strict; use warnings; use Data::Dumper; my ($Rec,$pc_name); undef $/; open (LST,"c:/tim/showtech".$ARGV[0].".txt") || die "$!\n"; my $Switch_Data =<LST>; close LST; my @Switch_Array = split(/\*{66}\n/, $Switch_Data); for (@Switch_Array) { next unless (/show port-channel database/); $pc_name = $1, next if (/(port-channel \d+)$/); $Rec->{$pc_name}->{$1} = $2 if (m|(fc\d+/\d+)\s+\[(\w+)\]|); #print "$_\n"; }
    the error I ma getting is
    Use of uninitialized value in hash element at C:\Perl\Vodafone\pm_data +_chunks2.p l line 20.
    Any Idea why?

    Blackadder