> -----Original Message-----
> From: Gareth Londt [mailto:[EMAIL PROTECTED]]
> Sent: Monday, October 01, 2001 10:54 AM
> To: [EMAIL PROTECTED]
> Subject: a new line?
> 
> 
> i need to know where to put a new line in.........i keep 
> getting errors......?
> here is what the code looks like, if anyone can help with the 
> newline....i 
> would much appreciate it.
> 
> #!/usr/bin/perl -w
> 
> $file = 'dnsbills.06-09-2001';
> 
> open FILE, $file;
> @lines = <FILE>;
> close (FILE);
> 
> for ($line=0;$line <=$#lines;$line++){
>         if ($lines[$line] =~ /^Reg Date/) {
>                 print STDERR $lines[$line+2];
>                 @columns = split " ", $lines[$line+2];
>                 print $columns[1]; 
>                 }
>         }
> 

You don't say what errors you're getting, but here's 
one way you might rewrite that is more "Perl-ish":

    open FILE, $file or die $!;    # check for error

    while (<FILE>) {
        next unless /^Reg Date/;
        $_ = <FILE>; $_ = <FILE>;  # read 2 lines ahead
        print STDERR;              # print whole line
        print((split)[1], "\n");   # print just 2nd field
    }

I added a newline on the last print statement. Was that your
original question? Anyway, this version:

1. Checks for failure of open()
2. Avoids all the intermedate data structures
3. Doesn't use more memory for bigger files
4. Is (arguably) cleaner

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

Reply via email to