blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I have this code

#! c:/perl/bin/perl.exe # use strict; use vars qw/@Showtech_Log/; print "\nScript: $0\n"; if (open(FILE,"c:\\showtech217.txt")) { $/='*' x 66; @Showtech_Log =<FILE>; close FILE; } else { die "\n$!\tc:\\showtech217.txt\n"; } ##################### Main Loop ###################### for my $data (@Showtech_Log) { my $Port_Channel; my $Port_Channel_Details; my $Interface; my $Interface_Details; ##################### Switch Name ####################### if ( $data =~ /show switchname -+\n(\w+)/) { print "\nSwitch Name: $1\n"; } ##################### Clock Time/Date ##################### if ($data =~ /show clock -+\n(.+)/) { print "\nClock: $1\n"; } ##################### Port-Channel DB ##################### if ($data =~ /show port-channel database/) { my @temp = split (/\n+/,$data); for (@temp) { $Port_Channel = $1 , next if (/port-channel (\d+)$/); $Port_Channel_Details->{$Port_Channel}->{$1} = $2 if (m|(fc\d ++/\d+)\s+\[(\w+)\]|); } } ##################### Running-Config ###################### if ($data =~ /show running-config/) { my @temp = split (/\n+/,$data); for (@temp) { $Interface = $1, next if (/interface port-channel (\d+)$/); $Interface_Details->{$Interface}->{$1} = $2 if (/switchport d +escription To (\w+) ([\d\.]+)/); } } for my $Port (keys %{$Port_Channel_Details}) { print "\nPort Channel $Port : \n"; for my $channels (keys %{$Port_Channel_Details->{$Port}}) { print "\t$channels [$Port_Channel_Details->{$Port}->{$channel +s}]\n"; } } for my $Interface_Port (keys %{$Interface_Details}) { print "\nInterface Port Channel $Interface_Port : \n"; for my $Interface_Channel (keys %{$Interface_Details->{$Interfac +e_Port}}) { print "\t$Interface_Channel : $Interface_Details->{$Interface +_Port}->{$Interface_Channel}\n"; } } ##################### FLOGI DB ######################### if ($data =~ /show flogi database/) { print "\n"; my @temp = split (/\n/,$data); for (@temp) { #print "$_\n" if (/^fc/); } } }

Which Producess this output:

Script: C:\Scripts\array3.pl Switch Name: CCC217_ANG_GREEN Clock: Fri Sep 23 11:14:42 UTC 2005 Port Channel 1 : fc2/5 [up] fc1/5 [up] Port Channel 3 : fc1/1 [up] Interface Port Channel 1 : CCC219 : 10.33.81.56 Interface Port Channel 3 : CCC215 : 10.33.81.52

Which is fine...However I am trying to tie up the Ports to their Interface channels. Normlly this bit of code does the trick for me:

for my $Port (keys %{$Port_Channel_Details}) { print "\nPort Channel $Port : \n"; for my $Interface_Port (keys %{$Interface_Details}) { next unless ($Port == $Interface_Port); print "$Interface_Port\n"; } }

On this occasion I get only the output from the outer loop but nothing from the inner loop! I have tried this many time before and it works fine! And I can't see anything wrong with this bit of code!

Perhaps an eagle eyed monk can help me out, and point as to where I am going wrong?

Thanks

Blackadder

Replies are listed 'Best First'.
Re: Simple things can be most difficult...Sometimes!
by Roy Johnson (Monsignor) on Oct 05, 2005 at 15:29 UTC
    Wouldn't it make more sense to write
    for my $Port (keys %{$Port_Channel_Details}) { print "\nPort Channel $Port : \n"; print "$Port\n" if exists $Interface_Details->{$Port}; }
    ? Not that it should do anything different.

    Caution: Contents may have been coded under pressure.
Re: Simple things can be most difficult...Sometimes!
by VSarkiss (Monsignor) on Oct 05, 2005 at 15:01 UTC
      But $Port and $Interface_Port are both digits!

      I am sure I have tried next unless ($Port eq $Interface_Port); But I will try again!!!

      ########## UPDATE\########

      Nope, got the same thing!
      Blackadder
Re: Simple things can be most difficult...Sometimes!
by 5mi11er (Deacon) on Oct 05, 2005 at 21:55 UTC
    Blackadder,

    From reading your various posts, it seems to me, a network guy, that you might be doing various things relating to routers/switches the hard way.

    VLANs, IP addresses, Channels (aka Trunks), Interfaces, Sub-Interfaces, MAC addresses, etc. are all inter-related and it seems to me that you are forever "finding" the related pieces via loops.

    I think if you planned how to lay all this out with intelligent data structures, a lot of the "work" that goes on in your script(s) would disappear.

    I'm offering to help layout all that stuff, but even after reading through you older posts, I'm not quite sure where you're headed with all this. Could you post a summary of what exactly you're trying to accomplish?

    -Scott

Re: Simple things can be most difficult...Sometimes!
by nedals (Deacon) on Oct 05, 2005 at 17:31 UTC
    There appears to be no data for the inner loop.
    ... @Showtech_Log =<FILE>; ## Get data line-by-line ... for my $data (@Showtech_Log) { ... my @temp = split (/\n+/,$data); ## But $data is only one line so there's no data for an inner loop for (@temp) { ..