Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: opening a b...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 10886 of 12261
Post > Topic >>

Re: opening a big file

by shawnhcorey@[EMAIL PROTECTED] (Mr. Shawn H. Corey) Apr 6, 2008 at 11:11 PM

On Sun, 2008-04-06 at 22:36 -0400, Richard Lee wrote:
> I am trying to open a big file and go through line by line while 
> limiting the resource on the system.
> What is the best way to do it?
> 
> Does below read the entire file and store them in memory(not good if 
> that's the case)..
> 
> open(SOURCE, "/tmp/file") || die "not there: $!\n";
> while (<SOURCE>) {
> ## do something
> }

The above code will read the file one line at a time.  It is recommended
that you use the three-argument open statement.  (See `perldoc -f
open`).

open( SOURCE, '<', "/tmp/file" ) || die "cannot open /tmp/file: $!\n";
while (<SOURCE>) {
  # do something
}

> 
> sometime ago I saw somewhere it had something like below which look like

> it was reading them and going through line by line without storing them 
> all in memory.
> I just cannot remember the syntax exactly.
> 
> open(SOURCE, " /tmp/file |") || die "not there: $!\n";
> while (<>) {
> ## do something
> 
> }

This syntax is is used for reading the output of another command.  It is
not recommended because your program would not be ****table across
operating systems.  But sometimes you have no choice.

open( SOURCE, '-|', "command" ) || die "cannot pipe from command: $!\n";
while (<SOURCE>) {
  # do something
}

Also see `perldoc IPC::Open2` and `perldoc IPC::Open3`


-- 
Just my 0.00000002 million dollars worth,
    Shawn

99% of you are just big dumb apes!

+------------\
| Shangri La   \
| 40,000 KM     /
+------------/
 




 2 Posts in Topic:
Re: opening a big file
shawnhcorey@[EMAIL PROTEC  2008-04-06 23:11:54 
Re: opening a big file
rich.japh@[EMAIL PROTECTE  2008-04-20 13:49:34 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 17:46:08 CST 2008.