You have declared @test2 inside an if block inside the first while loop. As soon as execution leaves the if block the array goes out of scope. See Coping with Scoping for more details.
Update: Example - compare these two:
use strict;
my $foo = 0;
for (1..5) { $foo += $_; }
print $foo;
and
use strict;
for (1..5) {
my $foo = 0;
$foo += $_;
}
print $foo;
In each case the scope of $foo ends at the end of the block enclosing its declaration. Since the first example has the declaration outside the loop the scope persists to the end of the script. In contrast, the second example gives $foo scope only until the end of the for loop thus making the $foo in the print statement undeclared which will be trapped at compile time.
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.