Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Why am I getting so many issues with REGEX!!!!

by blackadder (Hermit)
on Oct 15, 2005 at 11:01 UTC ( [id://500440] : perlquestion . print w/replies, xml ) Need Help??

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

Hi,...this is really driving me up the wall.

This is my code

$|++; AUTOLOAD; use strict; use warnings 'all'; if (open(FILE,"c:\\logfile.txt")) { print "\nExtracting Data from c:\\logfile.txt.......Please Wait: "; $/='*' x 66; my @ShowtechLog =<FILE>; close FILE; print "Done\n\n"; my ( $Interface, $InterfaceInfo, $SwitchPort, $SwitchDesc ); for my $data (@ShowtechLog) { if ($data =~ /show running-config/) { for (split (/\n+/,$data)) { $Interface = $1, next if (/interface port-channel (\d+)$/) +; $InterfaceInfo->{$Interface}->{$1} = $2 if (/switchport de +scription To (\w+) (&#91;\d\.&#93;+)/); $SwitchPort = $1, next if (/^interface (fc\d+\/\d+)$/); $SwitchDesc->{$SwitchPort} ={$1} if (/switchport descripti +on (.+)/); } } } print "\nPort Descriptions\n"; for my $Port (keys %{$SwitchDesc}) { print "$Port : "; for my $Desc (keys %{$SwitchDesc->{$Port }}) { print "$Desc\n"; } } } else { warn "\n$!\tc:\\logfile.txt\n"; }
And this is my data

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 interface mgmt0 ip address 10.33.81.54 255.255.240.0 interface fc1/1 switchport description Trunk switchport mode E channel-group 3 force no shutdown interface fc1/2 interface fc1/3 switchport description DMX01_FA3CA no shutdown interface fc1/4 switchport description DMX01_FA8BA no shutdown interface fc1/5 switchport description Trunk switchport mode E channel-group 1 force no shutdown interface fc1/6 interface fc1/7 interface fc1/8 switchport description DMX01_FA7BA no shutdown interface fc1/9 interface fc1/10 interface fc1/11 interface fc1/12 switchport description DMX01_FA4BA no shutdown interface fc1/13 interface fc1/14 switchport description DMX01_FA9BA no shutdown interface fc1/15 switchport description DMX01_FA7CA no shutdown interface fc1/16 switchport description DMX01_FA3BA no shutdown interface fc2/1 switchport mode E interface fc2/2 interface fc2/3 switchport description DMX01_FA4CA no shutdown interface fc2/4 switchport description DMX01_FA8AA no shutdown interface fc2/5 switchport description Trunk switchport mode E channel-group 1 force no shutdown interface fc2/6 interface fc2/7 switchport description DMX01_FA14BA no shutdown interface fc2/8 switchport description DMX01_FA7AA no shutdown interface fc2/9 interface fc2/10 interface fc2/11 no shutdown interface fc2/12 switchport description DMX01_FA4AA no shutdown interface fc2/13 interface fc2/14 interface fc2/15 switchport description DMX01_FA8CA no shutdown interface fc2/16 switchport description DMX01_FA3AA no shutdown interface fc3/1 switchport description misdr no shutdown interface fc3/2 no shutdown interface fc3/3 interface fc3/4 interface fc3/5 no shutdown interface fc3/6 no shutdown interface fc3/7 interface fc3/8 interface fc3/9 no shutdown interface fc3/10 no shutdown interface fc3/11 interface fc3/12 interface fc3/13 no shutdown interface fc3/14 no shutdown interface fc3/15 interface fc3/16 interface fc3/17 no shutdown interface fc3/18 switchport speed 2000 no shutdown interface fc3/19 interface fc3/20 interface fc3/21 no shutdown interface fc3/22 interface fc3/23 interface fc3/24 interface fc3/25 switchport description Tim Test no shutdown interface fc3/26 interface fc3/27 interface fc3/28 interface fc3/29 no shutdown interface fc3/30 interface fc3/31 interface fc3/32 interface fc4/1 no shutdown interface fc4/2 no shutdown interface fc4/3 interface fc4/4 interface fc4/5 no shutdown interface fc4/6 no shutdown interface fc4/7 interface fc4/8 interface fc4/9 no shutdown interface fc4/10 no shutdown interface fc4/11 interface fc4/12 interface fc4/13 no shutdown interface fc4/14 no shutdown interface fc4/15 interface fc4/16 interface fc4/17 no shutdown interface fc4/18 interface fc4/19 interface fc4/20 interface fc4/21 no shutdown interface fc4/22 interface fc4/23 interface fc4/24 interface fc4/25 no shutdown interface fc4/26 interface fc4/27 interface fc4/28 interface fc4/29 no shutdown interface fc4/30 interface fc4/31 interface fc4/32
And this is the errors I ma getting

Extracting Data from c:\logfile.txt.......Please Wait: Done Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Use of uninitialized value in hash element at C:\Perl\test.pl li ne 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Use of uninitialized value in hash element at C:\Perl\test.pl li ne 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Odd number of elements in anonymous hash at C:\Perl\test.pl line 33. Port Descriptions : To CCC215 10.33.81.52 fc2/7 : DMX01_FA14BA fc1/15 : DMX01_FA7CA fc2/16 : DMX01_FA3AA fc1/3 : DMX01_FA3CA fc1/14 : DMX01_FA9BA fc1/8 : DMX01_FA7BA fc2/3 : DMX01_FA4CA fc2/15 : DMX01_FA8CA fc1/16 : DMX01_FA3BA fc1/4 : DMX01_FA8BA fc2/4 : DMX01_FA8AA fc3/1 : misdr fc1/5 : Trunk fc1/1 : Trunk fc2/8 : DMX01_FA7AA fc2/5 : Trunk fc1/12 : DMX01_FA4BA fc3/25 : My Test fc2/12 : DMX01_FA4AA
My question is:

1- Why am I getting these warnings:

Odd number of elements in anonymous hash at C:\Perl\test.pl line 33.
And how can I get rid of them?

2- Why is the RegEc picking up lines starting with Interface but not followed by (fc\d+\/\d+) exactly!!!!

In the first regEx, I am saying : Pick interface port-channel (\d+)$/ followed by /switchport description To (\w+) (\d\.+)/ i.e. it must have the word "To" in there!

In the second reges I am saying: pick lines starting with Interface but don't have the word "To" - becouse its not there - So why is it picking it up!!!

3- whats the way forward? how can I seperate the 2 regex's? how can I rectify this sticky situation?

I understand that \. will gualify as \w and it will pick up lines with or without "To" . How can I make it more Precise!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks

Blackadder

Edit: g0n - added readmore tags
Edit: holli - restored node content


Updated content:
I ma very sorry about this,...But I just can't believe that I have been on this simple problem for WEEKS!!!!! And I cannot sort it out even with the help of PerlMonks,...This is almost impossible.

It frustrated me so much, although my problems have not been sorted but out I had to withdraw my post..

Replies are listed 'Best First'.
Re: Why am I getting so many issues with REGEX!!!!
by eyepopslikeamosquito (Archbishop) on Oct 15, 2005 at 11:54 UTC

    The "Odd number of elements in anonymous hash" warning is caused by the line:

    $SwitchDesc->{$SwitchPort} ={$1} if (/switchport description (.+)/);
    The expression {$1} is creating an anonymous hash reference with a key of $1 but without a value (hence the "odd number" warning; hashes should contain an even number of list items). As it stands, the hash reference will have a value of undef. You can get rid of the warning by changing {$1} to, say, {$1,42} -- assuming you want the value for this key to be 42.

    Update: {$1,42} above is more clearly expressed with a fat comma: { $1 => 42 }.

Re: Why am I getting so many issues with REGEX!!!!
by holli (Abbot) on Oct 15, 2005 at 12:20 UTC
    Your program is, sorry, quite a mess. You are slurping in the whole file, run over the array and then you try to split a single! line at "\n".

    I suggest you specify the output/data-structure you want and let me/us try a rewrite.


    holli, /regexed monk/
      You are correct,Ö I understand itís messy, so I decided to abandon this approach in favor of the approach below:
      #! c:/perl/bin/perl.exe # # $|++; AUTOLOAD; use strict; use warnings 'all'; my $DataRec= {}; if (open(DATA,"c:\\logfile.txt")) { my ($pc_name, $Rec, $hash, $key, $Port, $PortDesc); print "\nExtracting data from c:\\logfile.txt\n\n"; while (<DATA>) { if (/^port-channel (\d+)$/) { $pc_name = $1; } elsif (m|(fc\d+/\d+)\s+\[(\w+)\]|) { $Rec->{$pc_name}->{$1} = $2; } elsif (/^interface port-channel (\d+)$/) { $key = $1; } elsif (/switchport description To (\w+) ([\d+\.]+)/) { $hash->{$key}->{$1} = $2; } #### Now I have a problem with the below ##### elsif (/^interface (fc\d+\/\d+)$/) { $Port = $1; } elsif (/\s+switchport description (.+)$/) { $PortDesc->{$Port} = $2 if ($2 ne ""); } ##################################### } for my $Port (keys %{$PortDesc}) { print "$Port : $PortDesc->{$Port}\n"; # no output for $PortDe +sc->{$Port}!!! } for my $Channel (keys %{$Rec}) { for my $Port (keys %{$Rec->{$Channel}}) { for my $Match (keys %{$hash}) { next unless ($Channel == $Match); $DataRec->{ISL}->{$Port}->{$_} = $hash->{$Match}->{$_} for + (keys %{$hash->{$Match}}); } } } &Create_Excel($DataRec); print "\n-------------------------\n"; Close DATA; } else { warn "$!\n"; } sub Create_Excel { my ($Record) = @_; print "\nISL Information \n"; for my $Port (keys %{$Record->{ISL}}) { print "$Port : "; for my $Name (keys %{$Record->{ISL}->{$Port}}) { print "$Name : $Record->{ISL}->{$Port}->{$Name}\n"; } } }
      It seems that my problem is that I can't get all the data that I need unless I close DATA and oopen it again! Surely I can get all that I need from the first pass through...Righht!...Thnaks
      Blackadder
Re: Why am I getting so many issues with REGEX!!!!
by graff (Chancellor) on Oct 15, 2005 at 13:33 UTC
    I'll bet you could have reduced the data by about 90% (making a much shorter, more readable post) and still be able to reproduce the problem.

    It would be nice to post a reply to your own node -- since the OP is already too long -- showing how you resolved the problem. (Who knows, maybe there's still another problem that isn't being tripped by the data you have on hand, and someone here will be able to spot it before it messes you up.)

    Please don't "withdraw" a post, especially after others have replied to it. It's not just for your own personal benefit that your stuff shows up on this web site. (But cleaning it up by removing irrelevant detail would be fine.)

      I am not a betting man,..More of a fuzzy logic man :-),..But I get your point.
      Blackadder
Re: Why am I getting so many issues with REGEX!!!!
by dws (Chancellor) on Oct 15, 2005 at 16:18 UTC

    I just can't believe that I have been on this simple problem for WEEKS!!!!!

    Have you tried looking to CPAN for help? It looks like you're parsing a configuration file for a router/switch, which is a problem well-solved by packages already available on CPAN. Taking an educated guess, Cisco-Reconfig might save you some work.

Re: Why am I getting so many issues with REGEX!!!!
by nedals (Deacon) on Oct 15, 2005 at 17:38 UTC

    In your initial post..

    I assume that the 'data' is a sample of the file content, but the data does NOT have a line that contains 'show running-config'. So looping through the data will do nothing, which will account for a number for errors.

    Your 2nd 'code' posting seems to be unrelated to the first.

    Perhaps it's time to take a step backward and more clearly define what it is you are trying to accomplish.

    sudo-code... Parse data and sort into a data structure. Data structure to be: ???? Print (show desired output).
Re: Why am I getting so many issues with REGEX!!!!
by g0n (Priest) on Oct 15, 2005 at 17:35 UTC
    If I understand aright, you are seeking to deal with the descriptions of interface definitions separately, depending on whether the descriptions contain "To". Yes?

    If so, this works:

    although it's a little krufty. Each interface is being pulled into a separate list element, then treated with multi line pattern matches. That seems to make it easier to separate them out cleanly. Hopefully that might get you a little closer to a solution

    Anyone care to improve on my code?

    --------------------------------------------------------------

    $perlquestion=~s/Can I/How do I/g;