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: Intermediate:
is this greedy?

 



achen
New User

Sep 30, 2009, 7:30 AM

Post #1 of 9 (2268 views)
is this greedy? Can't Post

here is a snippet of a data file i have:

=============================================================================
JDBC Runtimes rd7_datasource
=============================================================================
-r-- ActiveConnectionsAverageCount 0
-r-- ActiveConnectionsCurrentCount 0
-r-- ActiveConnectionsHighCount 5
-r-- ConnectionDelayTime 91
-r-- ConnectionsTotalCount 70
-r-- CurrCapacity 70
-r-- CurrCapacityHighCount 0
-r-- DeploymentState 2
-r-- Enabled true
-r-- FailedReserveRequestCount 0
-r-- FailuresToReconnectCount 0
-r-- HighestNumAvailable 70
-r-- HighestNumUnavailable 0
-r-- LastTask null
-r-- LeakedConnectionCount 0
-r-- ModuleId rd7_datasource
-r-- Name rd7_datasource
-r-- NumAvailable 70
-r-- NumUnavailable 0
-r-- PrepStmtCacheAccessCount 60337
-r-- PrepStmtCacheAddCount 6796
-r-- PrepStmtCacheCurrentSize 77
-r-- PrepStmtCacheDeleteCount 6719
-r-- PrepStmtCacheHitCount 53541
-r-- PrepStmtCacheMissCount 6796
-r-- Properties {user=spd_user}
-r-- ReserveRequestCount 86107
-r-- State Running
-r-- Type JDBCDataSourceRuntime
-r-- VersionJDBCDriver oracle.jdbc.OracleDriver
-r-- WaitSecondsHighCount 0
-r-- WaitingForConnectionCurrentCount 0
-r-- WaitingForConnectionFailureTotal 0
-r-- WaitingForConnectionHighCount 0
-r-- WaitingForConnectionSuccessTotal 0
-r-- WaitingForConnectionTotal 0
-r-- WorkManagerRuntimes null

=============================================================================
JDBC Runtimes fgrlog
=============================================================================
-r-- ActiveConnectionsAverageCount 0
-r-- ActiveConnectionsCurrentCount 0
-r-- ActiveConnectionsHighCount 0
-r-- ConnectionDelayTime 119
-r-- ConnectionsTotalCount 30
-r-- CurrCapacity 30
-r-- CurrCapacityHighCount 0
-r-- DeploymentState 2
-r-- Enabled true
-r-- FailedReserveRequestCount 0
-r-- FailuresToReconnectCount 0
-r-- HighestNumAvailable 30
-r-- HighestNumUnavailable 0
-r-- LastTask null
-r-- LeakedConnectionCount 0
-r-- ModuleId fgrlog
-r-- Name fgrlog
-r-- NumAvailable 30
-r-- NumUnavailable 0
-r-- PrepStmtCacheAccessCount 0
-r-- PrepStmtCacheAddCount 0
-r-- PrepStmtCacheCurrentSize 0
-r-- PrepStmtCacheDeleteCount 0
-r-- PrepStmtCacheHitCount 0
-r-- PrepStmtCacheMissCount 0
-r-- Properties {user=log_user}
-r-- ReserveRequestCount 0
-r-- State Running
-r-- Type JDBCDataSourceRuntime
-r-- VersionJDBCDriver oracle.jdbc.OracleDriver
-r-- WaitSecondsHighCount 0
-r-- WaitingForConnectionCurrentCount 0
-r-- WaitingForConnectionFailureTotal 0
-r-- WaitingForConnectionHighCount 0
-r-- WaitingForConnectionSuccessTotal 0
-r-- WaitingForConnectionTotal 0
-r-- WorkManagerRuntimes null



I want to extract each data source and its following attributes:

ActiveConnectionsAverageCount
ActiveConnectionsCurrentCount
ActiveConnectionsHighCount
WaitingForConnectionCurrentCount
WaitingForConnectionFailureTotal
WaitingForConnectionHighCount
WaitingForConnectionTotal

Here is my program:

#!/usr/bin/perl

$savedate = "";

open (LOG, "<test.log") or die "can't open log file: $!\n";

while (<LOG>) {
chomp;

if (/(^2009-09-29.*)/) {
$date = $1;
if ($savedate) {
print "--------------------------\n";
print "$savedate\n";
print "--------------------------\n";
print "$jvmruntime\n";
print "$heapfreepercent\n";
print "--------------------------\n";
print "$rd7_datasource\n";
print "$rd7_activeconnavgcount\n";
print "$rd7_activeconncurrcount\n";
print "$rd7_activeconnhighcount\n";
print "$rd7_waitingforconncurrcount\n";
print "$rd7_waitingforconnhighcount\n";
print "$rd7_waitingforconntotal\n";
print "--------------------------\n";
print "$fgrlog_datasource\n";
print "$fgrlog_activeconnavgcount\n";
print "$fgrlog_activeconncurrcount\n";
print "$fgrlog_activeconnhighcount\n";
print "$fgrlog_waitingforconncurrcount\n";
print "$fgrlog_waitingforconnhighcount\n";
print "$fgrlog_waitingforconntotal\n";
print "--------------------------\n";


$savedate = $date;
$jvmruntime = "";
$heapfreepercent = "";
$rd7_datasource = "";
$rd7_activeconnavgcount = "";
$rd7_activeconncurrcount = "";
$rd7_activeconnhighcount = "";
$rd7_waitingforconncurrcount = "";
$rd7_waitingforconnhighcount = "";
$rd7_waitingforconntotal = "";
$fgrlog_datasource = "";
$fgrlog_activeconnavgcount = "";
$fgrlog_activeconncurrcount = "";
$fgrlog_activeconnhighcount = "";
$fgrlog_waitingforconncurrcount = "";
$fgrlog_waitingforconnhighcount = "";
$fgrlog_waitingforconntotal = "";
next;
} else {
$savedate = $date;
next;
}
}

if (/(.*JVMRuntime.*)/) {
$jvmruntime = $1;
next;
}

if (/(.*HeapFreePercent.*)/) {
$heapfreepercent = $1;
next;
}

if (/(.*JDBC Runtimes rd7_datasource.*)/) {
$rd7_datasource = $1;
next;
}
if (/(.*ActiveConnectionsAverageCount.*)/) {
$rd7_activeconnavgcount = $1;
next;
}

if (/(.*ActiveConnectionsCurrentCount.*)/) {
$rd7_activeconncurrcount = $1;
next;
}

if (/(.*?ActiveConnectionsHighCount.*)/) {
$rd7_activeconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionCurrentCount.*)/) {
$rd7_waitingforconncurrcount = $1;
next;
}
if (/(.*WaitingForConnectionHighCount.*)/) {
$rd7_waitingforconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionTotal.*)/) {
$rd7_waitingforconntotal = $1;
next;
}

if (/(.*JDBC Runtimes fgrlog.*)/) {
$fgrlog_datasource = $1;
next;
}
if (/(.*ActiveConnectionsAverageCount.*)/) {
$fgrlog_activeconnavgcount = $1;
next;
}

if (/(.*ActiveConnectionsCurrentCount.*)/) {
$fgrlog_activeconncurrcount = $1;
next;
}

if (/(.*ActiveConnectionsHighCount.*)/) {
$fgrlog_activeconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionCurrentCount.*)/) {
$fgrlog_waitingforconncurrcount = $1;
next;
}
if (/(.*WaitingForConnectionHighCount.*)/) {
$fgrlog_waitingforconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionTotal.*)/) {
$fgrlog_waitingforconntotal = $1;
next;
}

}
close LOG;


Problem is, when I run the program, it finds "JDBC Runtimes rd7_datasource" and the picks up alll the attributes for "JDBC Runtimes fgrlog" instead. So I get the following output:

--------------------------
2009-09-29 22:54:41
--------------------------
-r-- Type JVMRuntime
-r-- HeapFreePercent 33
--------------------------
JDBC Runtimes rd7_datasource
-r-- ActiveConnectionsAverageCount 0
-r-- ActiveConnectionsCurrentCount 0
-r-- ActiveConnectionsHighCount 0
-r-- WaitingForConnectionCurrentCount 0
-r-- WaitingForConnectionHighCount 0
-r-- WaitingForConnectionTotal 0
--------------------------
JDBC Runtimes fgrlog






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

How can I stop this greedy behavior - so the rd7_datasource properties get selected for rd7_datasource, instead of the fgrlog properties?



Thanks for any help.


savo
User

Sep 30, 2009, 8:27 AM

Post #2 of 9 (2261 views)
Re: [achen] is this greedy? [In reply to] Can't Post

you need /(regex)?/ to make it none greedy but because its a while loop it will still match twice so you need a count to stop the second here is what i have now its nearly working.


Code
open (LOG, "<test.log") or die "can't open log file: $!\n"; 
my $c = 1;
while (<LOG>) {
chomp;
{
if ($c == 1) {
if (/(ActiveConnectionsHighCount?)/){
say; ++$c}
}
}}



(This post was edited by savo on Sep 30, 2009, 8:30 AM)


savo
User

Sep 30, 2009, 8:30 AM

Post #3 of 9 (2259 views)
Re: [savo] is this greedy? [In reply to] Can't Post

This should work.

Code
#!/usr/bin/perl 
use warnings;
use strict;
use 5.010;

open( LOG, "<test.log" ) or die "can't open log file: $!\n";
my $c = 1;
while (<LOG>) {
chomp;

if ( $c == 1 ) {
if (/(ActiveConnectionsHighCount?)/) {
say;
}

}

if (/JDBC Runtimes fgrlog/) {
++$c;
}

}



(This post was edited by savo on Sep 30, 2009, 8:32 AM)


achen
New User

Sep 30, 2009, 8:47 AM

Post #4 of 9 (2253 views)
Re: [savo] is this greedy? [In reply to] Can't Post

i understand your logic but now how to implement the syntax:

#!/usr/bin/perl

$savedate = "";

open (LOG, "<test.log") or die "can't open log file: $!\n";

my $c = 1;

while (<LOG>) {
chomp;

if ($c == 1) {

if (/(^2009-09-29.*)/) {
$date = $1;
if ($savedate) {
print "--------------------------\n";
print "$savedate\n";
print "--------------------------\n";
print "$jvmruntime\n";
print "$heapfreepercent\n";
print "--------------------------\n";
print "$rd7_datasource\n";
print "$rd7_activeconnavgcount\n";
print "$rd7_activeconncurrcount\n";
print "$rd7_activeconnhighcount\n";
print "$rd7_waitingforconncurrcount\n";
print "$rd7_waitingforconnhighcount\n";
print "$rd7_waitingforconntotal\n";
print "--------------------------\n";
print "$fgrlog_datasource\n";
print "$fgrlog_activeconnavgcount\n";
print "$fgrlog_activeconncurrcount\n";
print "$fgrlog_activeconnhighcount\n";
print "$fgrlog_waitingforconncurrcount\n";
print "$fgrlog_waitingforconnhighcount\n";
print "$fgrlog_waitingforconntotal\n";
print "--------------------------\n";


$savedate = $date;
$jvmruntime = "";
$heapfreepercent = "";
$rd7_datasource = "";
$rd7_activeconnavgcount = "";
$rd7_activeconncurrcount = "";
$rd7_activeconnhighcount = "";
$rd7_waitingforconncurrcount = "";
$rd7_waitingforconnhighcount = "";
$rd7_waitingforconntotal = "";
$fgrlog_datasource = "";
$fgrlog_activeconnavgcount = "";
$fgrlog_activeconncurrcount = "";
$fgrlog_activeconnhighcount = "";
$fgrlog_waitingforconncurrcount = "";
$fgrlog_waitingforconnhighcount = "";
$fgrlog_waitingforconntotal = "";
next;
} else {
$savedate = $date;
next;
}
}

if (/(.*JVMRuntime.*)/) {
$jvmruntime = $1;
next;
}

if (/(.*HeapFreePercent.*)/) {
$heapfreepercent = $1;
next;
}

if (/(.*JDBC Runtimes rd7_datasource.*)/) {
$rd7_datasource = $1;
next;
}

if (/(.*ActiveConnectionsAverageCount.*)/) {
$rd7_activeconnavgcount = $1; ++c;
next;
}

if (/(.*ActiveConnectionsCurrentCount.*)/) {
$rd7_activeconncurrcount = $1; ++c;
next;
}

if (/(.*ActiveConnectionsHighCount.*)/) {
$rd7_activeconnhighcount = $1; ++c;
next;
}

if (/(.*WaitingForConnectionCurrentCount.*)/) {
$rd7_waitingforconncurrcount = $1; ++c;
next;
}
if (/(.*WaitingForConnectionHighCount.*)/) {
$rd7_waitingforconnhighcount = $1; ++c;
next;
}

if (/(.*WaitingForConnectionTotal.*)/) {
$rd7_waitingforconntotal = $1; ++c;
next;
}

if (/(.*JDBC Runtimes fgrlog.*)/) {
$fgrlog_datasource = $1;
next;
}
if (/(.*ActiveConnectionsAverageCount.*)/) {
$fgrlog_activeconnavgcount = $1;
next;
}

if (/(.*ActiveConnectionsCurrentCount.*)/) {
$fgrlog_activeconncurrcount = $1;
next;
}

if (/(.*ActiveConnectionsHighCount.*)/) {
$fgrlog_activeconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionCurrentCount.*)/) {
$fgrlog_waitingforconncurrcount = $1;
next;
}
if (/(.*WaitingForConnectionHighCount.*)/) {
$fgrlog_waitingforconnhighcount = $1;
next;
}

if (/(.*WaitingForConnectionTotal.*)/) {
$fgrlog_waitingforconntotal = $1;
next;
}
}
}
close LOG;


I don't want to stop the count after getting the values for rd7_datasource; I need to get the values for fgrlog datasource too. I tried to fit in the counter as you stated, but am just getting compilation errors.



Thanks in advance.


savo
User

Sep 30, 2009, 9:19 AM

Post #5 of 9 (2252 views)
Re: [achen] is this greedy? [In reply to] Can't Post

Your code prints nothing for me so cant just update that but this should help


Code
#!/usr/bin/perl 
use warnings;
use strict;
use 5.010;

my $rd7_activeconnhighcount;
my $fgrlog_activeconnhighcount;

open (LOG, "<test.log") or die "can't open log file: $!\n";
my $c = 1;
while (<LOG>) {
chomp;

if ($c == 1) {
$rd7_activeconnhighcount = $1 if /(ActiveConnectionsHighCount?)/
} else {
$fgrlog_activeconnhighcount = $1 if /(ActiveConnectionsHighCount?)/
}


if (/JDBC Runtimes fgrlog/) {
++$c}

}


say $rd7_activeconnhighcount;

say $fgrlog_activeconnhighcount;


It will print
ActiveConnectionsHighCount
ActiveConnectionsHighCount

I have to go now so cant finish it.

If you work it out let me know how if not i will try looking again later or tomorrow.

I am new to perl but could a hash be used here rather than all the variables?


(This post was edited by savo on Sep 30, 2009, 9:21 AM)


savo
User

Sep 30, 2009, 4:51 PM

Post #6 of 9 (2235 views)
Re: [savo] is this greedy? [In reply to] Can't Post

Almost there, i can get all the data and i can print it but it will print for all the rd7 then all the fgrlog.

I cant think any more so maybe someone else could point the obvious out to us.

Just had a thought, there is probably a way to put it all in one hash and then print not with it now though so will give up for a bit.

Edit

removed code as to long and had errors


(This post was edited by savo on Oct 1, 2009, 12:48 PM)


savo
User

Oct 1, 2009, 3:28 AM

Post #7 of 9 (2226 views)
Re: [achen] is this greedy? [In reply to] Can't Post

I had a thought this morning which turned out to work.

I have used a list to save the parts to be printed and had the rd7 to count in 2's starting from 0 and the frlog from 1 so when printed it is printing correctly.

Would love to see how this should have been done if any of the others can let us know it would be good but here is my final working attempted until i learn some more.
Edit

removed code as to long and had errors


(This post was edited by savo on Oct 1, 2009, 12:49 PM)


savo
User

Oct 1, 2009, 4:30 AM

Post #8 of 9 (2221 views)
Re: [savo] is this greedy? [In reply to] Can't Post

realised that the date is always printed so came up with this regex to remove it where not needed.



Code
for (@list) { 
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\s+[A|W]/) {
s/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\s+[A|W]/ /g;
}
s/ c/ Ac/g if (/ c/);
s/ a/ Wa/g if (/ a/);
}

print @list;



savo
User

Oct 1, 2009, 12:50 PM

Post #9 of 9 (2205 views)
Re: [savo] is this greedy? [In reply to] Can't Post

Here is an actual working version.


Code
#!/usr/bin/perl 
use warnings;
use strict;

my %rd7;
my %fgrlog;
my $date;
my $ActiveConnectionsAverageCount;
my $HeapFreePercent;
my $ActiveConnectionsCurrentCount;
my $ActiveConnectionsHighCount;
my $WaitingForConnectionCurrentCount;
my $WaitingForConnectionTotal;
my $WaitingForConnectionHighCount;
my @list;
my $c = 0;
my $cc = 1;

open( LOG, "<test.log" ) or die "can't open log file: $!\n";

while (<LOG>) {
chomp;
if (/(^2009-09-29.*)/) {
$date = $1;
}

if (/^-r--/) {
my ( $dump, $type, $count ) = split / +/;

if ( !exists $rd7{$date}{$type} ) {
$rd7{$date}{$type} = $count;
}
else {
$fgrlog{$date}{$type} = $count;

}
}
}

for my $date ( sort keys %rd7 ) {
for my $type ( sort keys %{ $rd7{$date} } ) {
if ( $type =~ /ActiveConnectionsAverageCount/i ) {
$ActiveConnectionsAverageCount = "$date $type $rd7{$date}{$type}";
}
if ( $type =~ /HeapFreePercent/i ) {
$HeapFreePercent = "$date $type \t\t\t\t$rd7{$date}{$type}";
}
if ( $type =~ /ActiveConnectionsCurrentCount/i ) {
$ActiveConnectionsCurrentCount =
"$date $type \t\t\t$rd7{$date}{$type}";
}
if ( $type =~ /ActiveConnectionsHighCount/i ) {
$ActiveConnectionsHighCount =
"$date $type \t\t\t$rd7{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionCurrentCount/i ) {
$WaitingForConnectionCurrentCount =
"$date $type \t\t$rd7{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionHighCount/i ) {
$WaitingForConnectionHighCount =
"$date $type \t\t\t$rd7{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionTotal/i ) {
$WaitingForConnectionTotal = "$date $type \t\t\t$rd7{$date}{$type}";
$list[$c] =
"--------------------------\n"
. "$date\n"
. "--------------------------\n"
. "-r-- Type\t\t\t\t\t\t\tJVMRuntime\n"
. "$HeapFreePercent\n"
. "JDBC Runtimes rd7_datasource\n"
. "$ActiveConnectionsCurrentCount\n"
. "$ActiveConnectionsHighCount\n"
. "$WaitingForConnectionCurrentCount\n"
. "$WaitingForConnectionHighCount\n"
. "$WaitingForConnectionTotal\n" . "\n";
$c += 2;
}

}
}

for my $date ( sort keys %fgrlog ) {
for my $type ( sort keys %{ $fgrlog{$date} } ) {
if ( $type =~ /ActiveConnectionsAverageCount/i ) {
$ActiveConnectionsAverageCount = "$date $type $fgrlog{$date}{$type}";
}
if ( $type =~ /ActiveConnectionsCurrentCount/i ) {
$ActiveConnectionsCurrentCount =
"$date $type \t\t\t$fgrlog{$date}{$type}";
}
if ( $type =~ /ActiveConnectionsHighCount/i ) {
$ActiveConnectionsHighCount =
"$date $type \t\t\t$fgrlog{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionCurrentCount/i ) {
$WaitingForConnectionCurrentCount =
"$date $type \t\t$fgrlog{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionHighCount/i ) {
$WaitingForConnectionHighCount =
"$date $type \t\t\t$fgrlog{$date}{$type}";
}
if ( $type =~ /WaitingForConnectionTotal/i ) {
$WaitingForConnectionTotal = "$date $type \t\t\t$fgrlog{$date}{$type}";
$list[$cc] =
"--------------------------\n"
. "$date\n"
. "--------------------------\n"
. "-r-- Type\t\t\t\t\t\t\tJVMRuntime\n"
. "JDBC Runtimes fgrlog\n"
. "$ActiveConnectionsCurrentCount\n"
. "$ActiveConnectionsHighCount\n"
. "$WaitingForConnectionCurrentCount\n"
. "$WaitingForConnectionHighCount\n"
. "$WaitingForConnectionTotal\n" . "\n";
$cc += 2;
}

}
}

for (@list) {
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\s+[A|W]/) {
s/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\s+[A|W]/ /g;
}
s/ c/ Ac/g if (/ c/);
s/ a/ Wa/g if (/ a/);
}

print @list;


 
 


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

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