My code is object oriented..
$mon_obj->{'FILE_DATA'} is a reference to an array, each element of the array represents a line in a text file. Periodically I delete elements(lines in the file) by setting them to undef(or I could use
delete $array[$x]). After deleting a number of elements, I wanted to do a sort of 'garbage collection' with the elements that are set to undef.
Below is the best method I could come up with for a way that actually deletes the element. It seems easier and better(to me) to ensure the undef array elements are tossed out, as opposed to adding a
defined($mon_obj->{'FILE_DATA'}->[$x]) check within every structure in my code that loops over this array.
Does all this seem reasonable? Do share any/all insights.
thanx.
my $x;
my $counter;
for($x = 0; $x < @{$mon_obj->{'FILE_DATA'}}; $x++)
{
unless($mon_obj->{'FILE_DATA'}->[$x]) ## if undef incr. the gap
{
$counter++;
}
if($counter > 0) ## copy the data from $x + gap to $x
{
$mon_obj->{'FILE_DATA'}->[$x] = $mon_obj->{'FILE_DATA'}->[$x + $c
+ounter];
}
}
for($x = 0; $x < $counter; $x++)
{
pop @{$mon_obj->{'FILE_DATA'}; ## pop into oblivion
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.