If you want to understand what's going on, you can
try adding some print statements. This should make
it pretty clear what's happening.
use strict;
my @list = qw(1 2 3 4 5);
my $each = "";
my $i = 0;
my @newlist = ();
foreach $each (@list) {
print "Each was: $each\n"; #new
$each *= 2;
print "Each is: $each\n"; #new
$newlist[$i] = $each;
print "New list is: $newlist[$i]\n"; #new
print "Old list is $list[$i]\n\n"; #new
++$i;
}
for ($i= 0; $i<5; ++$i) {
print ($newlist[$i] - $list[$i], "\n");
}
And maybe in the future it would be a good idea for the
reason for what happens to be included in the 'black box
of enlightenment' so that people won't just be puzzled,
but will learn, too? (Or maybe not; it's good debugging
practice to figure out where the unexpected happens.
Why have the box at all? There isn't much point to just
show the output, since people can just run the
program to see the output.)
-
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.
|