It's not clear to me exactly what you're trying to do, but you've sinned against a big no-no:
Do not modify an array in a foreach loop over the same array.
What I do, if I don't mind the array being eaten in the process, is something like this:
$\ = "\n"; # append newline after print
@a = 'a';
while(@a) { # there's stuff left in the array
my $item = shift @a; # remove it, ready to process
print $item;
if(rand() < 0.8) {
# sometimes add a new item
push @a, ('a' .. 'z')[int rand 26];
}
}
Example output:
a
p
i
n
o
q
e
p
k
You see? while doesn't mind at all if the item to loop over, changes under it. foreach does.
That's one way that you can, for example, replace a recursive implementation of a file digger that does something like File::Find, with an iterative one: just push new directories you encounter while processing a directory, onto the to-do array.
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.