I don't have the original email, so I'm having to reply to the original
question here (see the end of the email), as well as the response.


On Sat, Jul 28, 2001 at 01:56:10PM -0400, Akshay Arora wrote:
> @temp=<TEMP>; #implies that TEMP is a read file handle
> print TEMP @temp; #implies that TEMP is a write file handle.
> 
> I'm pretty sure that you can only do either read or write, but not both
> to the same FILE HANDLE. You can open 2 handles to the same file (which
> can cause a few problems...), but if you are trying to write to a 2nd
> file, use a different file handle

A filehandle can be opened for reading and writing.  On some operating
systems you have to seek between reads and writes, but that's not the
problem here, or, at least, that's not the problem causing the warning he's
seeing.


> open(TEMP,"<file"); #read mode
> open(TEMP2,">>file"); #append mode

open(TEMP, "+<file"); # read-write non-clobber mode




> COLLINEAU Franck FTRD/DMI/TAM wrote:
> >
> > this code doesn't work:
> > 
> >         @temp=<TEMP>;
> >         $temp[2]="";
> >         $temp[3]="";
> >         $temp[4]="";
> >         $temp[5]="";
> >         print TEMP @temp;
> > 
> > The error message is "Use of uninitialized value in print at 01_info.pl line
> > 25, <TEMP> line 1."

Some element of @temp is undefined, and you should know this after having
read perldiag for a description of this error message.  The only reason an
element of @temp can be undefined is if the file is one line long or less.
In this case, the problem comes with the first assignment.  Consider:

    @temp = <TEMP>; # @temp = ("foo\n");
    $temp[2] = "";  # @temp = ("foo\n", undef, "");

When you assign to a specific index in an array that is beyond the current
length of the array, the length of the array is increased, and the
intervening elements from the initial length to the new length are given a
value of undef.

Splicing out the elements you don't want is one way of solving this problem,
but it will be slow for large lengths of @temp.  Reading a file into an
array is usually a bad idea anyways, so perhaps you should be using
something along the lines of:

    use File::Copy qw(move);
    open(IN,  "<$orig_file") || die("...");
    open(OUT, ">$tmp_file" ) || die("...");

    while (<IN>) {
        print OUT unless $. >= 3 && $. <= 6;
    }

    move($tmp_file, $orig_file) || die("...");

Be sure to obtain a safe value for $tmp_file.  See perldoc -q 'line in a
file' (for tips on editing files in Perl), perldoc -q 'temporary file name'
(for instructions on how to open a temporary file), perldoc perlvar (for
$.), and perldoc File::Copy.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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

Reply via email to