Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

out of memory

by maddfisherman (Sexton)
on Aug 23, 2001 at 00:08 UTC ( [id://107115]=note: print w/replies, xml ) Need Help??


in reply to errors revisited

new script: now it gives error out of memory help?
#!/usr/bin/perl -w #use CGI::Carp('fatalsToBrowser'); #use Carp; #use diagnostics; use strict; #use warnings; my @pages; my @xfilenames; my @filenames; my @xtitles; my @titles; my @xheadings; my @headings; my @xstuff; my $xstuff=""; my @stuff; my @xtemplate; my $xtemplate=""; my @template; open(PAGES, "<pages.txt") || die $!; @pages = <PAGES>; close(PAGES); foreach (@pages) { if (m/^\*{4}filename.ext\*{4}/) { push(@xfilenames, $_) } elsif (m/^\*{4}title\*{4}/) { push(@xtitles, $_) } elsif (m/^\*{4}heading\*{4}/) { push(@xheadings, $_) } else { push(@xstuff, $_) } } foreach (@xfilenames) { s/^\*{4}filename.ext\*{4}//; chomp ($_); push(@filenames, $_); } foreach (@xtitles) { s/^\*{4}title\*{4}//; chomp ($_); push(@titles, $_); } foreach (@xheadings) { s/^\*{4}heading\*{4}//; chomp ($_); push(@headings, $_); } $xstuff = join("", @xstuff); @stuff = $xstuff =~ /\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; open(TEMPLATE, "<template.html") || die $!; @xtemplate = <TEMPLATE>; close (TEMPLATE); $xtemplate = join("", @xtemplate); @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/sg; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/sg); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/sg); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/sg); for (my $i=0; $i < (@filenames); $i++) { open(FILE, ">@filenames[$i]") || die $!; print FILE qq($temlate[0] $title[$i] $template[1] $heading[$i] $te +mplate[2] $stuff[$i] $template[3]); close(FILE); } print "Content-type:text/html\n\n"; print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>create</title> </head> <body> EndHTML foreach (@filenames) { print "<p>Name:$_</p>"; } print "<hr />"; foreach (@titles) { print "<p>Titles:$_</p>"; } print "<hr />"; foreach (@headings) { print "<p>Headings:$_</p>"; } print "<hr />"; foreach (@stuff) { print "<p>stuff:$_</p>"; } print <<EndHTML; </body> </html> EndHTML

Replies are listed 'Best First'.
Re: out of memory
by dragonchild (Archbishop) on Aug 23, 2001 at 00:18 UTC
    For one thing, you can cut out a number of arrays by combining what you're doing. From a quick glance, you don't seem to be using @xfilenames (and the like) except as a temporary data structure. Instead, do something like:
    my @filenames; my @titles; my @headings; my @stuff; foreach (@pages) { chomp; if (/^\*{4}filename.ext\*{4}/) { s/^\*{4}filename.ext\*{4}//; push @filenames, $_; } elsif (m/^\*{4}title\*{4}/) { s/^\*{4}title\*{4}//; push @titles, $_; } elsif (m/^\*{4}heading\*{4}/) { s/^\*{4}heading\*{4}//; push @headings, $_; } else { s/\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; push @xstuff, $_; } }
    You now have 4 less lists, thus cutting your memory usage for data structures almost in half.

    Note - don't declare all your variables up-front. That's a very C-like thing to do. Declare them when you need them, as you need them. Declaring a list or hash takes up considerable memory, as Perl will pre-declare like 50 (or so) elements that just sit there.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found