|
| Author |
Message |
jdles Newbie

Joined: 04 Aug 2005 Posts: 3
         
|
Posted: Fri Aug 05, 2005 2:28 am Post subject: File input |
|
|
| I'm new to Perl and trying to figure out ways to read things from a file. I can get the whole thing with @data=<DAT>; , or a character at a time with getc(), but are there any other options? I'll be reading pretty big files. Can I read until the next newline, for instance? Or until I see a ' '? |
|
| Back to top |
|
 |
draget Not Yet a God

Joined: 29 Dec 2004 Posts: 367 Location: Australia
   
|
Posted: Fri Aug 05, 2005 9:20 am Post subject: |
|
|
think loops.
i'd read it all into an array, then just loop through the array,. |
|
| Back to top |
|
 |
eric256 The Keymaker

Joined: 03 May 2006 Posts: 2292 Location: Colorado
     
|
Posted: Fri Aug 05, 2005 2:04 pm Post subject: |
|
|
You can read it all into your @data array and then loop into it. However you might end up with some memory concerns that way if the file is large. In those cases you can read line by line.
Will read a single line of data. It will return undef when you reach the end of the file....so you can do
| Code: | while (my $line = <DATA>){
# do something with each $line here
}
|
And that will read every line of the file, executing the middle code once for each line. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com |
|
| Back to top |
|
 |
darkmonkey The Merovingian

Joined: 18 Apr 2004 Posts: 2557 Location: London, England
     votes: 7
|
Posted: Thu Aug 11, 2005 10:04 pm Post subject: |
|
|
With big files, look into using the "tie" module. That's more advance than newb though. _________________ ~ Josh
[ Need bot hosting on a dedicated server? PM me. ] |
|
| Back to top |
|
 |
|