Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It hard to see how it could use quite so much memory, but there are a couple of obvious ways to reduce what it does use.

First, you are building an array to hold all the names of the text files in each directory, when all you need is the count. So you should just count in a loop. I notice that the readdir documentation in perlfunc only shows an example of readdir in list context, but for this purpose, a loop is better. If there is some huge directory with 100,000 files, that could use a lot of memory, but that seems unlikely.

Second, although this is probably a minor point, where you say foreach $key (sort keys %category), the sort is causing an array of all the keys to be built. Since this is unnecessary for this use, you can just leave the sort out.

#!/usr/bin/perl use Carp; use strict; use vars qw(%config %category %form %super); require "/var/www/vhosts/mysite.com/cgi-bin/categories.cgi"; $config{'basepath'} = '/var/www/vhosts/mysite.com/cgi-bin/'; $config{'bluedir'} = 'register'; sub count_text_files { my ($dirname) = @_; my $result = 0; if (!opendir DIR, $dirname) { carp "Cannot open $dirname: $!\n"; return 0; } while (defined(my $file = readdir DIR)) { if (-T "$dirname/$file") { ++$result; } } close DIR; return $result; } my $key; my $numusers = 1; $numusers += count_text_files("$config{'basepath'}$config{'bluedir'}") +; my $totalfiles = 100; foreach $key (keys %category) { $totalfiles += count_text_files("$config{'basepath'}$key"); }

In reply to Re: Poorly written script by Thelonius
in thread Poorly written script by Baffled

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.