[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Out of memory messages



>I first made a copy of the hostinfo.dat file so I don't mess it up, then I
>tried to read the file into an array, like so:
>
>#!/user/local/bin/perl -w
>
>print "Please select a file to view: ";
>chomp($file_name = <STDIN>);
>
>open(FILE, "$file_name");
>
>@newsgroups = <FILE>;
>print @newsgroups;
>
>close FILE;
>
>gives me an out of memory message.

The following script will read one line at a time, avoiding to read the 
entire file in memory :

 #!/user/local/bin/perl -w
 
 print "Please select a file to view: ";
 chomp($file_name = <STDIN>);
 
 open(FILE, "$file_name");
 
 while (defined ($newsgroup = <FILE>)) {  # read one line at a time

    print $newsgroup;

    # or do whatever you want with a newsgroup line
 }
 
 close FILE;

The <> "diamond" operator will read an entire file if in an array context 
("@newsgroup = <FILE>"), assigning one line to each elements, or will 
read one line in a scalar context ("$newsgroup = <FILE>").

Don't forget that the line delimiter is defined by the "input record 
separator" variable $/. By default, it's newline, which on the Mac is 
return. 

You may have to change this variable if your files comes from UNIX or 
DOS/Windows. If the $/ definition doesn't correspond to the file's 
content, one of the common problem is that a "$one_line = <FILE>" will 
read an entire file into a scalar variable...


>Also, I tried to split a range of array elements
>
>@goodgroups = split /,/, @newsgroups[12.. $#newsgroups]
>
>but the script hasn't let me get far enough along to know whether this will
>return what I'm after.

I don't use Netscape, so I don't know what's the line content. Could you 
post an example ?

Georges Martin

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch