perlquestion
jesuashok
Hi all
<p>
<br>
I have added piece of code and the Dumper Result of Hash of
Hash of Hash.
<br>
=> Creating Hash of Hash of Hash
<br>
=> Printing Hash of Hash of Hash
<br>
<br>
<br>
<b>Could anyone analyse my code and tell me how I can </b><b>minimise in accesing this hash and creating this hash?</b>
<br>
<b>Pleae advice me that whethere my code is sufficient or not.</b>
<br>
<b>Please add your advice on this code To improve the performance.</b>
<br>
<br>
<br>
Since, I got lot efficient Tips for my earlier posts, I have the hope that I will get for this writeup also.
<readmore>
__DATA__
<code>
PREV:RNC6.200507112330.30.RNCpm.pp
0000A15&&DO_RNC_SOURCE|130|0|20050712000000|AvgA13HoDelayPriorSessionSourceRncPerf|0
0000A15&&DO_RNC_SOURCE|130|0|20050711234500|AvgA13HoDelayPriorSessionSourceRncPerfashok|0
0000A15&&DO_RNC_SOURCE|130|0|20050712000000|AvgA13HoDelaySourceRncPerf|0
0000N15&&DO_RNC_SOURCE|130|0|20050712000000|IpAddressSourceRncPerf|0.0.0.0
0000X15&&DO_RNC_SOURCE|130|0|20050712000000|MaxA13HoDelayPriorSessionSourceRncPerf|0
0000X15&&DO_RNC_SOURCE|130|0|20050712000000|MaxA13HoDelaySourceRncPerf|0
0000M15&&DO_RNC_SOURCE|130|0|20050712000000|MinA13HoDelayPriorSessionSourceRncPerf|0
0000M15&&DO_RNC_SOURCE|130|0|20050712000000|MinA13HoDelaySourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectInvalidReasonPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectInvalidReasonSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeAttrMissingPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeAttrMissingSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeAttrNotRecognizedPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeAttrNotRecognizedSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeNotRecognizedPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectProtSubtypeNotRecognizedSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectSessionNotAuthenticPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectSessionNotAuthenticSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectSessionNotFoundPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13RejectSessionNotFoundSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13ReqTimeoutPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13ReqTimeoutSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13TotalRejectPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumA13TotalRejectSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffAttemptsPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffAttemptsSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureATInitiatedClosePriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureATInitiatedCloseSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureAtIdResponseFailurePriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureAtIdResponseFailureSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureAtIdTimeoutPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureAtIdTimeoutSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureHdwIdTimeoutPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureHdwIdTimeoutSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureInvalidHdwIdTypePriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureInvalidHdwIdTypeSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureInvalidHdwIdValuePriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureInvalidHdwIdValueSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureInvalidUatiCmpltSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureMiscPriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureMiscSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureNoRncResourceSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureNoUatiCmpltSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureNoUatiReqSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureRNCInitiatedClosePriorSessionSourceRncPerf|50
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureRNCInitiatedCloseSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureRetrievedConfigUnacceptablePriorSessionSourceRncPerf|0
0000R15&&DO_RNC_SOURCE|130|0|20050712000000|NumDormantHandoffFailureRetrievedConfigUnacceptableSourceRncPerf|0
</code>
The whole code is given :-
<code>
#! /usr/bin/perl
##!/usr/local/bin/perl -w
#
#
# remediation_1xEVDO.pl
#
# This script is used to implement data remediation for Nortel 1xEVDO
#
# Usage: remediation_1xEVDO.pl <config filename> <DC filename>
#
#
# Example:
# remediation_1xEVDO.pl NortelEVDO.cfg MTX41.1.20031029070000.1xEVDO
#use File::Basename;
use Time::Local;
#Global Variables
%basic_references = ();
%raw_hash = ();
$inFilePath = "RNC6.200507120000.30.RNCpm.pp1";
$current_filename = `basename $inFilePath`;
open (CDF,$inFilePath) or die "Cannot open $currDcFile:$!\n";
($temp1,$current_time_stamp,$temp2) = ($current_filename =~ /(\w+).(\w+).(.*)/);
$previous_file_name = get_previous_filename($temp1,$current_time_stamp,$temp2);
form_basic_reference($current_time_stamp);
for $file_name ( $previous_file_name, $inFilePath ) {
open ( CDF , "$file_name" ) || die "Can't open file $file_name :$!:\n";
while ( $line = <CDF> ) {
chomp($line);
($time_reference,$actual_data) = split('&&',$line);
$rem_type = substr($time_reference,4,1);
if ( grep(/^$time_reference$/,keys %basic_references) ) {
$section = $basic_references{$time_reference};
next if ($line =~ /^PREV/);
@origRec = split(/\|/, $actual_data);
$CDFEntity = shift @origRec;
$PegValue = pop @origRec;
$PegName = pop @origRec;
$stTime = pop @origRec;
@InstID = @origRec;
$currEntPeg = join ("-", $CDFEntity, $PegName);
$InstanceKey=join ("-",$PegName, @InstID);
$main_hash{$CDFEntity}{$rem_type}{$InstanceKey}{$section} = [ $PegValue,$stTime,substr($time_reference,5,2) ];
$main_hash{$CDFEntity}{$rem_type}{'allnegativeflag1'} = 'false';
$main_hash{$CDFEntity}{$rem_type}{'allnegativeflag2'} = 'false';
}
}
}
use Data::Dumper;
foreach $val (keys %main_hash) { #Entity
foreach $type ( keys %{$main_hash{$val}} ) { #rem_type
$total_R_count = keys %{$main_hash{$val}{'R'}};
#print "Rem_count :$total_R_count:\n";
$diff_negative_count_2 = 0; $diff_negative_count_1 = 0;
foreach $pegs ( keys %{$main_hash{$val}{$type}} ) {
$T1val = defined ${$main_hash{$val}{$type}{$pegs}{'Section1'}}[0] ? ${$main_hash{$val}{$type}{$pegs}{'Section1'}}[0] : undef;
$T2val = defined ${$main_hash{$val}{$type}{$pegs}{'Section2'}}[0] ? ${$main_hash{$val}{$type}{$pegs}{'Section2'}}[0] : undef;;
$T3val = defined ${$main_hash{$val}{$type}{$pegs}{'Section3'}}[0] ? ${$main_hash{$val}{$type}{$pegs}{'Section3'}}[0] : undef;
if ( $type eq 'R' ) {
$diffT1 = $T2val - $T1val if (defined $T2val && defined $T1val);
$diffT2 = $T3val - $T2val if (defined $T3val && defined $T2val);
##
if ($diffT1 < 0) {
$diff_negative_count_1++;
if ($T1val > (2**32 - 500000000)) {
$diffT1 = 2**32 - $T1val + $T2val;
} else {
$diffT1 = $T2val;
}
}
if ($diffT2 < 0) {
$diff_negative_count_2++;
if ($T2val > (2**32 - 500000000)) {
$diffT2 = 2**32 - $T2val + $T3val;
} else {
$diffT2 = $T3val;
}
}
##
$main_hash{$val}{$type}{$pegs}{'diff1'} = defined $diffT1 ? $diffT1 : 'Y';
$main_hash{$val}{$type}{$pegs}{'diff2'} = defined $diffT2 ? $diffT2 : 'Y';
} else {
$main_hash{$val}{$type}{$pegs}{'diff1'} = defined $T1val ? $T1val : 'Y';
$main_hash{$val}{$type}{$pegs}{'diff2'} = defined $T2val ? $T2val : 'Y';
}
}
$main_hash{$val}{$rem_type}{'allnegativeflag1'} = 'true' if ($total_R_count == $diff_negative_count_1 );
$main_hash{$val}{$rem_type}{'allnegativeflag2'} = 'true' if ($total_R_count == $diff_negative_count_2 );
#print "Rem_count :$total_R_count: $diff_negative_count_1 : $diff_negative_count_2\n";
}
}
#Final String Formation
foreach $val (keys %main_hash) { #Entity
foreach $type ( keys %{$main_hash{$val}} ) { #rem_type
$printed_1 = 0; $printed_2 = 0;
if ( $type eq 'R' && $main_hash{$val}{$type}{'allnegativeflag1'} eq 'true' ) {
print "# Warning: No remediations for First section of $val. System might be restarted ?\n";
$printed_1 = 1;
}
if ( $type eq 'R' && $main_hash{$val}{$type}{'allnegativeflag2'} eq 'true' ) {
print "# Warning: No remediations for Second section of $val. System might be restarted ?\n";
$printed_2 = 1;
}
next if ( $printed_1 == 1 && $printed_2 == 1 );
if ($printed_1 == 0 && $printed_2 == 0) {
foreach $pegs ( keys %{$main_hash{$val}{$type}} ) {
next if ($pegs =~ /allnegativeflag/);
@returnKeyT2 = split (/-/, $pegs);
$pegName = shift @returnKeyT2;
$finalStringT1 = join ("\|",$val,@returnKeyT2,${$main_hash{$val}{$type}{$pegs}{'Section1'}}[1],$pegName,$main_hash{$val}{$type}{$pegs}{'diff1'});
$finalStringT2 = join ("\|",$val,@returnKeyT2,${$main_hash{$val}{$type}{$pegs}{'Section2'}}[1],$pegName,$main_hash{$val}{$type}{$pegs}{'diff2'});
#print "$finalStringT1\n";
#print "$finalStringT2\n";
}
}
if ($printed_1 == 1 && $printed_2 == 0) {
foreach $pegs ( keys %{$main_hash{$val}{$type}} ) {
next if ($pegs =~ /allnegativeflag/);
@returnKeyT2 = split (/-/, $pegs);
$pegName = shift @returnKeyT2;
$finalStringT2 = join ("\|",$val,@returnKeyT2,${$main_hash{$val}{$type}{$pegs}{'Section2'}}[1],$pegName,$main_hash{$val}{$type}{$pegs}{'diff2'});
#print "$finalStringT2\n";
}
}
if ($printed_1 == 0 && $printed_2 == 1) {
foreach $pegs ( keys %{$main_hash{$val}{$type}} ) {
next if ($pegs =~ /allnegativeflag/);
@returnKeyT2 = split (/-/, $pegs);
$pegName = shift @returnKeyT2;
$finalStringT1 = join ("\|",$val,@returnKeyT2,${$main_hash{$val}{$type}{$pegs}{'Section1'}}[1],$pegName,$main_hash{$val}{$type}{$pegs}{'diff1'});
#print "$finalStringT1\n";
}
}
}
}
print Dumper [ %main_hash ];
#####################
# This function forms the Basic references, which is created using the Timestamp
# for which the process goes
# sub form_basic_reference
# input : timestamp
#####################
sub form_basic_reference {
my $dateStr = shift;
$Current0 = $dateStr;
$Prev1 = create_reference( $dateStr , 'sub',15);
$Prev2 = create_reference( $Prev1 , 'sub',15);
$Current1 = create_reference( $dateStr , 'add',15);
$Current2 = create_reference( $Current1 , 'add',15);
$prev1 = substr($Prev1,8,4);
$prev2 = substr($Prev2,8,4);
$current0 = substr($Current0,8,4);
$current1 = substr($Current1,8,4);
$basic_references{$prev1. 'N'.'15'} = 'Section1';
$basic_references{$prev1. 'R'.'15'} = 'Section1';
$basic_references{$prev2. 'N'.'30'} = 'Section1';
$basic_references{$prev2. 'R'.'30'} = 'Section1';
$basic_references{$current0.'N'.'15'} = 'Section2';
$basic_references{$current0.'R'.'15'} = 'Section2';
$basic_references{$current0.'N'.'30'} = 'Section2';
$basic_references{$current0.'R'.'30'} = 'Section2';
$basic_references{$current1.'N'.'15'} = 'Section3';
$basic_references{$current1.'R'.'15'} = 'Section3';
}
#####################
# This function forms the returns new timestamp based on the input timestamp
# and the operation it takes as a parameter
# sub create_reference
# input : timestamp sub|add
#####################
sub create_reference {
my $dateStr = shift;
my $operation = shift;
my $interval = shift;
#print "input :$dateStr:\n";
my $sec = 0;
my ($year, $mth, $day, $hr, $min) =
$dateStr =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)$/;
# Get seconds since the epoch, note months start from zero for
# January to 11 for December. Add 15 minutes-worth of seconds.
#
my $timeVal = timelocal($sec, $min, $hr, $day, $mth - 1, $year);
$timeVal += $interval * 60 if $operation eq 'add';
$timeVal -= $interval * 60 if $operation eq 'sub';
# Turn back in to separate parts, year, month etc. Note that
# localtime() returns years since 1900. Construct a new date
# string and return it.
#
my @timeParts = localtime($timeVal);
my $newStr =
$timeParts[5] + 1900 .
sprintf("%02d", $timeParts[4] + 1) .
sprintf("%02d", $timeParts[3]) .
sprintf("%02d", $timeParts[2]) .
sprintf("%02d", $timeParts[1]);
return $newStr;
}
#####################
# This function returns the previous filename
# by getting the current filename details
# sub get_previous_filename
# input : <first_part_of_file_name> <time_stamp> <third_part_of_file_name>
#####################
sub get_previous_filename {
$temp1 = shift;
$time_stamp = shift;
$temp2 = shift;
return $temp1.'.'.create_reference(create_reference($time_stamp,'sub'),'sub').'.'.$temp2;
}
</code>
</readmore>
</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-395537">
"Keep pouring your ideas"
</div></div>