perl pra wrote:
> hi gurus,

Hello,

> I have problem in type casting, here  is the problem..

That is not your problem.

> I get two values  after greping and spliting a file( the values are some
> thing like this "367" and "522>\n")

Your problem is the quotes (") are not numbers so perl is complaining.

> fetching of those two values from file would be in loop ..
> 
> now for each interation i have to compare these values and return the
> maximum  (i..e present values with the values of previous interation)
> 
> i tried comparing them by converting into integers (ii.e int("367")) i am
> not able to do it ....
> 
> I am getting the following error
> 
> 
> INDEX INITIALLY : "421"
> Argument ""188"" isn't numeric in int at E:\XYZ\ZBC\tt.pl line 50.
> 
> INDEX  SUB : "188"
> INDEX MAIN : "188"
> SEARCH INITIALLY : "141">
> 
> Argument ""312">\n" isn't numeric in int at E:\XYZ\ZBC\tt.pl line
> 59.
> SEARCH SUB : "312">
> 
> SEARCH MAIN : "312"
> Here is my code :
> 
> 
> 
> #!/usr/bin/perl -w
> 
> use strict;
> use Switch;
> my $i=1;
> my $INDEXMAX="";
> my $SEARCHMAX="";
> my @MAXISCNO="";
> my @MAXSSCNO="";
> my %var="";
> my $maxiscno="";
> my $maxsscno="";
> 
> sub MAX_ELAPSE{
> 
> 
> 
> my $count=$_[1];my $log=$_[2];
> my $logdir=$log . "logs";
> my $logfile=$log . "$count" . ".log";
> my $file="E:/XYZ/ZBC/Logs/$logdir/$logfile";
> my $indexmaxvar="";
> my $searchmaxvar="";
> open LOGFILE,"$file";

You should *ALWAYS* verify that the file opened correctly!

open LOGFILE, '<', $file or die "Cannot open '$file' $!";


> my @grepNames=grep(/(TimeTaken)/, <LOGFILE>);

You are asking the regular expression to do extra work and capture the pattern
to the $1 variable.


> close(LOGFILE);
> foreach my $s (@grepNames) {
> 
> my @words=split(/ /, $s);

split(/ /) splits on a *single* space character.  You probably want split(' ')
instead.


> foreach my $w (@words) {
> 
>  if ($w=~m/(docsAdded|msElapsedTime)/) {

You are asking the regular expression to do extra work and capture the pattern
to the $1 variable.


>     %var=split(/=/,$w);

You are assigning to the entire hash so at the end of the loop it will only
contain one key and one value (the last key and value encountered.)  If you
want to store more than one key and one value then:

      my ( $key, $val ) = split /=/, $w;
      $var{ $key } = $val;

And while here you can remove the quotes:

      ( $var{ $key } = $val ) =~ tr/"//d;

Or:

      ( $var{ $key } ) = $val =~ /(\d+)/;


>  } }
> }
> switch ($log) {

$log only ever contains "data" or "result" so your two cases will never execute.


> case "index"
> 
> {
>     $indexmaxvar=$_[0];
>  print "INDEX INITIALLY : $indexmaxvar\n";
>          foreach my $arr (keys (%var)) { if (int($indexmaxvar) <=

%var only contains one key and one value so you don't really need a loop.


> int($var{$arr})) {  $indexmaxvar=$var{$arr};$maxiscno=$count; print"INDEX
> SUB : $indexmaxvar\n";}
>                return ($indexmaxvar,$maxiscno); }

You are returning from inside the foreach loop so only the first $var{$arr}
will be returned.


> }
> case "search"
> {
>       $searchmaxvar=$_[0];
>  print "SEARCH INITIALLY : $searchmaxvar\n";
>        foreach my $arr (keys (%var)) {if (int($searchmaxvar) <=
> int($var{$arr})) {  $searchmaxvar=$var{$arr};$maxsscno=$count; print"SEARCH
> SUB : $searchmaxvar\n";}
>        return ($searchmaxvar,$maxsscno); }
>    }
> 
> }
> 
> }
> 
> 
> while ($i<=26) {

You may want to use a foreach loop for that:

foreach my $i ( 1 .. 26 ) {


> print "________________________________________SCENARIO NUMBER  $i
> START_________________________________\n";

Instead of typing in a lot of repeated characters:

print '_' x 40, "SCENARIO NUMBER  $i START", '_' x 33, "\n"


> ($INDEXMAX,$MAXISCNO)=MAX_ELAPSE($INDEXMAX,$i,"data");
> print "INDEX MAIN : $INDEXMAX\n";
> ($SEARCHMAX,$MAXSSCNO)=MAX_ELAPSE($SEARCHMAX,$i,"result");

You are passing "data" and "result" as the third argument but your switch is
looking for "index" and "search".


> print "SEARCH MAIN : $SEARCHMAX\n";
> print "________________________________________SCENARIO NUMBER  $i
> END________________________________\n";
> 
> $i = $i +1;
> 
> }


This *may* work better for you:


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


my %type = (
    index  => 'INDEX',
    search => 'SEARCH',
    );

sub MAX_ELAPSE {
    my ( $maxvar, $count, $log, $type ) = @_;

    return $maxvar unless exists $type{ $type };

    my $file = "E:/XYZ/ZBC/Logs/${log}logs/$log$count.log";

    open my $LOGFILE, '<', $file or return $maxvar;

    my %var;
    while ( <$LOGFILE> ) {
        next unless /TimeTaken/;
        for ( split ) {
            next unless /docsAdded|msElapsedTime/;
            my ( $key, $val ) = /^([^=]+)=\D*(\d+)/;
            $var{ $key } = $val;
            }
        }
    close $LOGFILE;

    print "$type{ $type } INITIALLY : $maxvar\n";
    for my $arr ( values %var ) {
        if ( $maxvar < $arr ) {
            $maxvar = $arr;
            print "$type{ $type } SUB : $maxvar\n";
            }
        }
    return $maxvar;
    }


my $INDEXMAX  = 0;
my $SEARCHMAX = 0;
for my $i ( 1 .. 26 ) {
    $INDEXMAX  = MAX_ELAPSE( $INDEXMAX,  $i, 'data',   'index' );
    $SEARCHMAX = MAX_ELAPSE( $SEARCHMAX, $i, 'result', 'search' );
    print '_' x 40, "SCENARIO NUMBER  $i START", '_' x 33, "\n",
        "INDEX MAIN : $INDEXMAX\n",
        "SEARCH MAIN : $SEARCHMAX\n",
        '_' x 40, "SCENARIO NUMBER  $i END", '_' x 33, "\n";
    }

__END__




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to