Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Eating RAM problem

by chromatic (Archbishop)
on Aug 01, 2002 at 17:43 UTC ( [id://186857]=note: print w/replies, xml ) Need Help??


in reply to Eating RAM problem

sub by_string { my $file = shift; local *IN, $/; open( IN, $file ) or die "Cannot open '$file': $!"; return <IN>; }

Access each element with substr. Memory savings? Several bytes per character, because Perl doesn't have to create a new SV for each character.

Replies are listed 'Best First'.
Re: Re: Eating RAM problem
by TheFifthDeuce (Novice) on Aug 01, 2002 at 18:08 UTC
    Chromatic, I need to get each element into an array. If the file size is 2 million bytes, then the array should have 2 million elements. How can I do that without draining RAM? Using your sub I get:
    @blah = by_string($file); $i = 0; foreach(@blah){ $i ++; } print $i; # Prints 1... not what I am looking for
    Thanks

      Then use length $blah[0] instead. You can do anything with strings that you can do with an array of 0's and 1's. The syntax is just a little different.

      Ron Steinke rsteinke@w-link.net
      Why do you need to get each element into an array?
      Could you use Tie::VecArray; ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://186857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found