CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Array Ouput Problem

 



popeye
Novice

Feb 18, 2014, 12:02 PM

Post #1 of 6 (1111 views)
Array Ouput Problem Can't Post


Code
 push @info, "$1 $2\n" if $version_info =~ /(right).+?(\*[^\n]+)/ms; 
if (defined $2) {
my $rver = "$2";
$rver =~ s/\*(.*)CTS //g;
$rver =~ s/ [A-Z].* $//g;
if ( $rver ne $ctsver ) {
push @SHVERERRS, "$rver,"; }
else {
push @SHVERERRS, "," ;
}
}
##
push @info, "$1 $2\n" if $version_info =~ /(presentation).+?(\*[^\n]+)/ms;
if (defined $2) {
my $prver = "$2";
$prver =~ s/\*(.*)CTS //g;
$prver =~ s/ [A-Z].* $//g;
if ($prver ne $ctsver) {
#print "POPEYE $prver\n";
push @SHVERERRS, "$prver,"; }
else {
push @SHVERERRS, "," ;
}
}


Fish monger helped me with code. Was working great until I started playing with it due to a new requirement.

The problem is when I print out @SHVERERRS the print doesn have either the value of "$prver," or ",", it has the value of the previous routine var $rver.

Ive been trying to figure this out for days !!

Any help would be appreciate.


Zhris
Enthusiast

Feb 18, 2014, 2:25 PM

Post #2 of 6 (1098 views)
Re: [popeye] Array Ouput Problem [In reply to] Can't Post

Hi,

From your description of the problem, I can imagine that the "if defined $2" condition doesn't fall true in the second segment, therefore nothing is pushed into @SHVERERRS. I can't tell you why it doesn't fall true without some data.

Could you provide a standalone script that makes use of the code you provided in your original post, replicates the issue you have encountered, and can be executed without having to write any additional code.

For example:


Code
use strict; 
use warnings;
use Data::Dumper;

my @info;
my @SHVERERRS;
my $ctsver = '*fooCTS blah';
my $version_info;

$version_info = 'right *fooCTS';


push @info, "$1 $2\n" if $version_info =~ /(right).+?(\*[^\n]+)/ms;
if (defined $2)
{
my $rver = "$2";
$rver =~ s/\*(.*)CTS //g;
$rver =~ s/ [A-Z].* $//g;

if ( $rver ne $ctsver )
{
push @SHVERERRS, "$rver,";
}
else
{
push @SHVERERRS, "," ;
}
}

$version_info = 'presentation *fooCTS';

push @info, "$1 $2\n" if $version_info =~ /(presentation).+?(\*[^\n]+)/ms;
if (defined $2)
{
my $prver = "$2";
$prver =~ s/\*(.*)CTS //g;
$prver =~ s/ [A-Z].* $//g;

if ($prver ne $ctsver)
{
#print "POPEYE $prver\n";
push @SHVERERRS, "$prver,";
}
else
{
push @SHVERERRS, "," ;
}
}

print Dumper \@info, \@SHVERERRS;


Output:


Code
$VAR1 = [ 
'right *fooCTS
',
'presentation *fooCTS
'
];
$VAR2 = [
'*fooCTS,',
'*fooCTS,'
];


Chris


(This post was edited by Zhris on Feb 18, 2014, 2:42 PM)


popeye
Novice

Feb 19, 2014, 4:40 AM

Post #3 of 6 (1076 views)
Re: [Zhris] Array Ouput Problem [In reply to] Can't Post

Sure ...

Example data

Quote
dmin:show version
primary
Factory CTS 1.9.6(2) P1
Slot 1 CTS 1.7.1(4864) P1
*Slot 2 CTS 1.9.6(2) P1
loads file information
Factory (cmterm-CTS.1-9-6-2R-K9.P1)
CTS: CTS.1-9-6-2R-K9.P1.sbn
Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
Slot 1
No loads file
Slot 2 (cmterm-CTS.1-9-6-2R-K9.P1)
CTS: CTS.1-9-6-2R-K9.P1.sbn
Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
left
Factory CTS 1.9.6(2) P1
Slot 1 CTS 1.7.1(4864) P1
*Slot 2 CTS 1.9.6(2) P1
right
Factory CTS 1.9.6(2) P1
Slot 1 CTS 1.7.1(4864) P1
*Slot 2 CTS 1.7.6(2) P1



Code
#!/usr/bin/perl 
#
use strict;
use Data::Dumper;
#
my $section;
my $hstr;
my $ip;
#
my @SHVERERRS;
my @SHSTATERRS;
my @SHHARDERRS;
my @SHUPERRS;
$"="";
#
my %dispatch = ( version => \&parse_version,
status => \&parse_status,
hardware => \&parse_hardware,
upgrade => \&parse_upgrade,
);

####################################


my $ctsver = "1.9.6(2)";
my $touchver = "CTSDEV.1-9-6-2R-K9.P1.SPA";


########################################
# temp out file
open(ERR, ">Errorlog") or die ("Unable to open Errorlog ");
#
foreach my $logfile ( glob "10*.log" ) {
my $nam = `grep Host $logfile`;
$nam =~ s/\s+/ /g;
$nam = (split/ /,$nam)[3] ;
$ip = substr($logfile,0,-4);
$hstr = "$nam,$ip";
print "$hstr\n";
print '-' x 30, "\n";
local $/ = "\nadmin:show ";
open my $fh, '<', $logfile or die "failed to open '$logfile' <$!>";
while (<$fh>) {
next unless /^(version)\b/;
chomp;
($section) = /^([a-z]+)\b/;
print "Parse show $section\n", '-' x 20, "\n";
my $code_ref = $dispatch{$section};
$code_ref->($_);
}
print '*' x 70, "\n";
print "\n\n";
close $fh;
}
#
sub parse_version {
my $version_info = shift;
my @info;
#
push @info, "$1 $2\n" if $version_info =~ /(primary).+?(\*[^\n]+)/ms;
my $pver = "$2";
$pver =~ s/\*(.*)CTS //g;
$pver =~ s/ [A-Z].* $//g;
push @SHVERERRS , "$hstr,";
if ( $pver ne $ctsver ) {
push @SHVERERRS, "$pver,";
}
else {
push @SHVERERRS, ",";
}
#
push @info, "$1 $2\n" if $version_info =~ /(left).+?(\*[^\n]+)/ms;
if (defined $2) {
my $lver = "$2";
$lver =~ s/\*(.*)CTS //g;
$lver =~ s/ [A-Z].* $//g;
if ( $lver ne $ctsver ) {
push @SHVERERRS, "$lver,";
}
else {
push @SHVERERRS, ",";
}
}
##
push @info, "$1 $2\n" if $version_info =~ /(right).+?(\*[^\n]+)/ms;
if (defined $2) {
my $rver = "$2";
$rver =~ s/\*(.*)CTS //g;
$rver =~ s/ [A-Z].* $//g;
if ( $rver ne $ctsver ) {
push @SHVERERRS, "$rver,";
}
else {
push @SHVERERRS, "," ;
}
}
##
push @info, "$1 $2\n" if $version_info =~ /(presentation).+?(\*[^\n]+)/ms;
if (defined $2) {
my $prver = "$2";
$prver =~ s/\*(.*)CTS //g;
$prver =~ s/ [A-Z].* $//g;
if ($prver ne $ctsver) {
push @SHVERERRS, "$prver,";
}
else {
push @SHVERERRS, "," ;
}
}
#

#push @info, "$1 $2\n" if $version_info =~ /(Touch:)\s+([^\n]+)/;
#if (defined $2) {
# my $tver = "$2";
# if ( $tver ne $touchver) {
# push @SHVERERRS, "$tver,"; }
#else {
#push @SHVERERRS, "," ;
#}
#}





print "@info\n";
push @SHVERERRS, "\n";

}

#
#do prints of arrays
unless (!defined @SHVERERRS) {
print ERR ",,SHOW VERSION RESULTS,,\n";
print ERR "Hostname,IP,Primary,Left,Right,Presentation,Touch\n";
print ERR "@SHVERERRS";





Example output

Quote
Host,ip,primary,left,right,presentation,touch
cow,10.9.1.203,,,
dog,10.9.1.204,,,1.7.6(2),1.7.6(2),,
duck,10.9.1.214,,,

What Im after
Host,ip,primary,left,right,presentation,touch
cow,10.9.1.203,,,,,,
dog,10.9.1.204,,,1.7.6(2),,,
duck,10.9.1.214,,,,,,

I go out to a list of net devices. Pull back commands and dump into a .log file.

Then I parse logs to compare user defined version numbers (currently setup statically). In order to test, I edit the log file to force failures/mismatches. In this case. I edited 10.9.1.204 version to read 1.7.6(2) under "right". Any mismatch should be printed, "version," else just a comma "," as a place holder as the Errorlog will eventually be opened in Excel as a csv. I think the problem is right routine but I don see it.

Thanks for the help.


popeye
Novice

Feb 19, 2014, 5:32 AM

Post #4 of 6 (1073 views)
Re: [popeye] Array Ouput Problem [In reply to] Can't Post

 
Ahhh ... this wont work.

If the left, right, presentation doesnt exist (not defined) then I will never hit the check for the version thus the mismatch or comma is never written to the array.


Zhris
Enthusiast

Feb 19, 2014, 10:07 AM

Post #5 of 6 (1060 views)
Re: [popeye] Array Ouput Problem [In reply to] Can't Post

Sounds like you have identified the problem, let us know if you require any further assistance.


popeye
Novice

Feb 19, 2014, 1:04 PM

Post #6 of 6 (1051 views)
Re: [Zhris] Array Ouput Problem [In reply to] Can't Post

 
Well I fixed part of the problem... but it looks like "right" versions/status is being held. Still cant figure out why thats happening. The rewritten sub is as follows :


Code
sub parse_version { 
my $version_info = shift;
my @info;
#
#Do Primary
push @info, "$1 $2\n" if $version_info =~ /(primary).+?(\*[^\n]+)/ms;
my $pver = "$2";
$pver =~ s/\*(.*)CTS //g;
$pver =~ s/ [A-Z].* $//g;
push @SHVERERRS, "$hstr,";
if ( $pver ne $ctsver ) { push @SHVERERRS, "$pver,";
} else {push @SHVERERRS, "p," }

#Do Left
push @info, "$1 $2\n" if $version_info =~ /(left).+?(\*[^\n]+)/ms;
if (defined $2) {
my $lver = "$2";
$lver =~ s/\*(.*)CTS //g;
$lver =~ s/ [A-Z].* $//g;
if ( $lver ne $ctsver ) { push @SHVERERRS, "$lver,"; } else { push @SHVERERRS, "l," }
}
if (!defined $2) { push @SHVERERRS, "l,"; }

#Do Right
push @info, "$1 $2\n" if $version_info =~ /(right).+?(\*[^\n]+)/ms;
if (defined $2) {
my $rver = "$2";
$rver =~ s/\*(.*)CTS //g;
$rver =~ s/ [A-Z].* $//g;
if ( $rver ne $ctsver ) { push @SHVERERRS, "$rver,"; } else { push @SHVERERRS, "r," }
}
if (!defined $2) { push @SHVERERRS, "r,"; }

#Do Presentation
push @info, "$1 $2\n" if $version_info =~ /(presentation).+?(\*[^\n]+)/ms;
if (defined $2) {
my $popeye= "$2";
$popeye =~ s/\*(.*)CTS //g;
$popeye =~ s/ [A-Z].* $//g;
#print "$popeye -------------------------------------------------\n";
if ( $popeye ne $ctsver ) { push @SHVERERRS, "$popeye,"; } else { push @SHVERERRS, "pr," }
}
if (!defined $2) { push @SHVERERRS, "pr,"; }

push @SHVERERRS, "\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives