On Tue, Jan 15, 2002 at 06:23:28PM +0100, Martin wrote:
> im trying to make a script to read a textfile with the following format:"
> 
> TITLE
> Welcome to Carcinoma in Situ
> 
> "
> i want my script to recognice the TITLE tag and save the following line(s) in $TITLE:
> 
> heres what i have done:
> 
> while (<>) {
>   chomp;
>       if (/TITLE\s*([A-Za-z]+)/) {
>          $TITLE=$1;
>   }
> }
> print $TITLE;
> 
> upon script textfile.txt > outputfile i get this error:
> Use of uninitialized value in concatenation (.) or string in line (the one with 
>print $TITLE in it)
> 
> i think my regex is not doing what i want it to do??? why?
---end quoted text---

$/ is the record delimiter. It's default is "\n". Each line will be read
in thus:

line1: TITLE\n
line2: Welcome to Carcinoma in Situ\n

so your loop will say :
line1: yup got TITLE, um no later match for regex :( so $1 is unset.
line2: nope it doesn't match.

either set $/ to undef
or set a flag in the loop when the regex is matched and clear it when a
value is added:

while (<>){
        if ($next_line_is_title) { # this line is a title
                chomp;
                $TITLE=$_;
                $next_line_is_title=0;
        }
        /TITLE/ and $next_line is title;
}

HTH.
-- 
 Frank Booth - Consultant
Parasol Solutions Limited.
(www.parasolsolutions.com)

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

Reply via email to