smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for everyone's suggestions.

thraxil suggested that misused array's might be my problem. It seemed a good place to start

My goal is to save an array reference which contains, at most, 10 datapoints. Every 60 seconds I get the array reference and add to it (dropping older off first)

In addition to knowing if there is a problem in the code, I would appreciate "coding" suggestions as I am new to Perl.

my @tmp; my $cur_qty; if (!defined $Output{"host"}) { $cur_qty = 0; } else { @tmp = @{$Output{"host"}}; $cur_qty = @tmp; } if (!$cur_qty) { # No saved data, just assign it $tmp[0] = $data; } elsif ($cur_qty >= 10) { # Remove oldest shift @tmp; # Add newest push @tmp, $data; } else # Just append it { push @tmp, $data; } # Save it $Output{"host"} = \@tmp;

Replies are listed 'Best First'.
Re (tilly) 1: More on: memory usage/leaks
by tilly (Archbishop) on Dec 10, 2001 at 04:08 UTC
    That by itself should be fine. I would, however, write the above snippet as something more like:
    my $tmp_log = ($Output{host} ||= []); push @$tmp_log, $data; if (10 < @$tmp_log) { shift @$tmp_log; }
    However I have to wonder at your data structure. Do you have multiple hosts here and so mean $Output{$host}? Or is %Output full of different kinds of logs, and so you could factor out common code that manipulates each one? Similarly the variable $data seems rather uninformatively named. Most things in a program are data of one kind or another. What kind of data is this data? I would suggest a different structure, but from the snippet you provide I don't have enough to make a concrete suggestion. PS The ||= line I had may confuse. Depending on the skill level of your maintainers, you might be better off expanding that out into:
    unless (exists $Output{host}) { $Output{host} = []; } my $tmp_log = $Output{host};