Eri Mendz wrote:
> hello everyone,
>
> since im learning perl, i might as well harness its power to solve my
> small real-world problem. sample lines of my grep'ped ppp log goes
> like this:
>
> Jan 12 05:47:50 localhost pppd[8696]: Connect time 2.2 minutes.
> Jan 12 19:23:44 localhost pppd[9359]: Connect time 34.0 minutes.
> [..]
> Jan 17 14:12:06 localhost pppd[20127]: Connect time 12.6 minutes.
> Jan 17 17:17:12 localhost pppd[20398]: Connect time 21.1 minutes.
> [..]
> Feb  3 17:59:42 localhost pppd[22888]: Connect time 4.4 minutes.
> Feb  3 21:12:22 localhost pppd[23421]: Connect time 7.5 minutes.
>                                                    ^^^^^
> i want to extract the time field on each line and get the sum total
> each time i end my net session (sort of estimating my time online).

Sounds like a regex would be useful here. This

    /([\d.]+)\s+([a-z]+)\./i

will check that the line in question is in the expected format and
return the time value (a sequence of digits or a period) and the
units name (a sequence of letters).

> Have started my perl script like this:
>
> #!/usr/bin/perl -w
> use strict;
>
> while (<>){
>     my $string = $_;

Here you throw away all of the convenience of functions
defaulting to $_ as an implicit parameter.

>     my @string = split /\s+/, $string;
>     print "$string[7]\n";

Without using $string, these two lines could be written

    print (split)[7],"\n";
> }
> exit;

Not necessary if it's the last line of the source.

>
> this just prints the time field on each line until finish of loop. i
> tried using += operator to get sum but i end up getting errors. how
> do i
> do this? i checked my script with perl's -c flag and it says syntax OK
> but i get errors when i run the script in command line:

At a guess your report has header lines which don't have a column 8.
$string[7] would be 'undef' in this case.

> Use of uninitialized value in concatenation (.) or string at
> ./diamond-optr.pl line 15, <> line 20. Use of uninitialized value in
> concatenation (.) or string at ./diamond-optr.pl line 15, <> line 21.
> Use of uninitialized value in concatenation (.) or string at
> ./diamond-optr.pl line 15, <> line 22.

Same thing.

> this is printed in first 3 lines before printing the individual time
> fields. once i solve this problem i want the perl script to run each
> time i go offline, so i reckon this should be fired in ppp init file
> or something.  this i dont know as well. kindly help. my home box
> is mandrake 9 and using wvdial in console to go online. TIA.

Can't help with wvdial, but how about this as a script:

    while (<>) {
        next unless /([\d.]+)\s+([a-z]+)\./i;
        die "Unexpected units field" unless $2 eq 'minutes';
        my $time = $1;
        print $time."\n";
    }

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to