The code uses splice to remove the elements from the array but the size of the array is not adjusted in the foreach loop.
Below code would traverse the array based on the last index of the array. This works even after using warnings module without any error.
Here I am adjusting the for loop count based on your splice.
use warnings;
print "hello world\n";
my @colors = qw(red green blue yellow pink purple brown);
my @drop = qw(pink brown);
my $num = 0;
for (0..$#colors){
if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){
+
splice (@colors, $num, 1);
$num--;
}
$num++;
}
print "@colors \n";
-
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.
|