-----Original Message-----
From: Steve [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 26, 2002 5:21 PM
To: Mark Anderson
Cc: Beginners@Perl. Org
Subject: Re: regex question


>From what I have read from The Camel $1 should contain the matched text.
>From that matched text I want to then search for an additional string.

I have this in a file:

Connect time 8.3 minutes
Connect time 100.1 minutes
(etc)
$line gets the whole line and from there I wanna place the numerical time
into a variable, so that 8.3 would be placed into $sum and then 100.1 added
into it so the total would eventually be in $sum.

> [ please cc: me as I am on digest and I may miss a reply]
>
> I have the following snippet of code:
>
>         if($line=~/Connect\s/) {
>                 print $line;
>                 if($1=~/[0-9]/) {
>                         print "$1\n";
>       }
> }
>
> The first if is OK, I get what I need.  The second if though I get this
> when I try to run the script:
> Use of uninitialized value in pattern match (m//) at ./conn_time.pl line
19
> What I am looking to do is find a substring in $line
>
> I am using warnings,diagnostics and strict as well.
>
> Thanks

-----My Response-----
$1 holds the matched text if it's inside of parens.  Here is how I would
write the code for what you want to do:

# I'm assuming that $sum has already been initialized to 0

# if $line begins with the word "Connect" and then has
# 1 or more digits followed by a decimal followed by 1
# or more digits, capture the number in $1.

if ($line =~ /^Connect.*(\d+\.\d+)/) {
        $sum += $1;
}



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

Reply via email to