in reply to Another Challenge: Splitting data into chunks part2

I'm not exactly sure of your desired output format, but how's this:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper::Simple; my ($hash,$key); while(<DATA>) { $key = $1, next if (/interface port-channel (\d+)$/); $hash->{$key}->{$1} = $2 if (/switchport description To (\w+) ([\d +\.]+)/); } print Dumper($hash); __DATA__ vsan 1060 wwn 20:00:00:e0:69:41:26:e7 fcid 0x6d0d00 area dynamic vsan 1030 wwn 10:00:00:00:c9:28:29:44 fcid 0xa70001 dynamic vsan 1030 wwn 10:00:00:00:c9:28:2c:55 fcid 0xa70002 dynamic vsan 1060 wwn 50:06:0b:00:00:0b:3a:0e fcid 0x6d0e00 area dynamic vsan 1060 wwn 20:00:00:e0:69:41:3b:ca fcid 0x6d0f00 area dynamic vsan 1060 wwn 20:00:00:e0:69:40:d1:3f fcid 0x6d1000 area dynamic vsan 1060 wwn 20:00:00:e0:69:41:55:1f fcid 0x6d1100 area dynamic vsan 1060 wwn 20:00:00:e0:69:41:57:1e fcid 0x6d1200 area dynamic vsan 1060 wwn 10:00:00:00:c9:45:3a:21 fcid 0x6d0006 dynamic vsan 1060 wwn 20:00:00:e0:69:41:55:ff fcid 0x6d1300 area dynamic vsan 1050 wwn 50:06:0b:00:00:30:0d:f4 fcid 0x140100 area dynamic logging server 10.33.34.235 6 interface port-channel 1 switchport trunk allowed vsan 1000 switchport trunk allowed vsan add 1050 switchport description To CCC219 10.33.81.56 switchport mode E interface port-channel 3 switchport trunk allowed vsan 1000 switchport trunk allowed vsan add 1010 switchport trunk allowed vsan add 1050 switchport trunk allowed vsan add 1900 switchport description To CCC215 10.33.81.52 switchport mode E vsan database vsan 1050 interface port-channel 1 vsan 1060 interface fc1/3 vsan 1060 interface fc1/4 vsan 1060 interface fc1/8 vsan 1050 interface fc1/12 vsan 1060 interface fc1/14 vsan 1060 interface fc1/15 vsan 1010 interface fc1/16 vsan 1900 interface fc2/1 vsan 1060 interface fc2/3 vsan 1010 interface fc2/4 vsan 1050 interface fc2/7

Output:
$hash = { '1' => { 'CCC219' => '10.33.81.56' }, '3' => { 'CCC215' => '10.33.81.52' } };

Replies are listed 'Best First'.
Re^2: Another Challenge: Splitting data into chunks part2
by blackadder (Hermit) on Sep 21, 2005 at 16:00 UTC
    Yes, I have posted the wrong code!!!! It really has been very long day/night.

    Your code works, but how can I combine 2 loops into one? What I mean is this
    #! c:/perl/bin/perl.exe use strict; my ($Rec2, $pc_name); my ($hash,$key); open (LST,"c:/showtech".$ARGV[0].".txt") || die "$!\n"; while(<LST>) { $key = $1, next if (/interface port-channel (\d+)$/); $hash->{$key}->{$1} = $2 if (/switchport description To (\w+) ([\d +\.]+)/); } close LST; open (LST,"c:/showtech".$ARGV[0].".txt") || die "$!\n"; while(<LST>) { $pc_name = $1, next if (/(port-channel \d+)$/); $Rec2->{$pc_name}->{$1} = $2 if (m|(fc\d+/\d+)\s+\[(\w+)\]|); } for my $data (keys %{$Rec2}) { print "\n$data \n"; } for my $data (keys %{$hash}) { print "$data\n"; }
    I am sure I don't have to open the file twice. but not sure on how to do it. If I merge both loops then only one loop works!
    Blackadder
      If you change your loop to something like this is should work:
      my $iname; while(<LST>) { $iname=$1, next if /port-channel (\d+)/; next if !defined $iname; $hash->{$iname}->{$1} = $2 if (/switchport description To (\w+) ([\d\.]+)/); $Rec2->{$iname}->{$1} = $2 if (m|(fc\d+/\d+)\s+\[(\w+)\]|); }
      Note, there is one small difference with the results you got from your code. Im sure you can see it, and fix it if its important:-)

      gc