Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie problems with filehandles

Status
Not open for further replies.

dulux

Programmer
Sep 4, 2002
36
0
0
GB
Hi all,

I am trying to perform a very basic action - read the contents of a .txt file into an array. However, I am losing
the first line of the file, which I suspect is because this has already been read into $_.

After opening the file, is there any way I can return the filehandle to the start of the file and then use the following to read the entire file into the array, as planned?

while(<FILEHANDLE>)
{
@array=<FILEHANDLE>;
}
close(FILEHANDLE);
#job done

This is probably really easy, but the help is always appreciated.

Thanks


 
I found the answer in thread219-343340.

Thanks all anyway.

 
Hi,
if all you want to do is read the file into an array, you don't need a loop. Just do this:

open(FILEHANDLE, &quot;somefile.txt&quot;);
@array=<FILEHANDLE>;
close(FILEHANDLE);

If you want to process it in the loop and also save it to array:

open(FILEHANDLE, &quot;somefile.txt&quot;);
while(<FILEHANDLE>)
{
push @array, $_;

<do something processing>
}
close(FILEHANDLE);

Hope this helps.


Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top