I want to thank all of you for your help in understanding this problem. I went to bed thinking about it and I realized the point about how the removal of the element "pink" changes the first array, and how that impacts the rest of the code.
As a student I don't mind if I lose a few points for it not being exactly right, I will still turn in my first script even though I've learned more from you guys. It's my work and I'm ok with taking the grade I've earned.
So I didn't know that "printf debugging" was a term, but I had already tried that a few times. In the end I had just determined to keep the script as simple as possible as long as it did the thing.
Here is what I had done to see what was happening.
use warnings;
my @colors = qw(red green blue yellow pink purple brown);
my $count = @colors;
my @drop = qw(pink brown);
my $num = 0;
foreach $num (1..$count){
$num--;
print "$count \n";
print "$num \n";
print "$colors[$num] \n";
if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){
splice (@colors, $num, 1);
}
else {
print "$colors[$num] was not dropped from the array \n";
}
}
print "@colors \n";
Of course this left me with a lot of questions originally. Here is what prints for me.
7
0
red
red was not dropped from the array
7
1
green
green was not dropped from the array
7
2
blue
blue was not dropped from the array
7
3
yellow
yellow was not dropped from the array
7
4
pink
7
5
brown
7
6
Use of uninitialized value within @colors in concatenation (.) or stri
+ng at C:\Users\..ch7q4.pl line 10.
Use of uninitialized value in string eq at C:\Users\..ch7q4.pl line 11
+.
Use of uninitialized value in string eq at C:\Users\..ch7q4.pl line 11
+.
Use of uninitialized value within @colors in concatenation (.) or stri
+ng at C:\Users\..ch7q4.pl line 15.
was not dropped from the array
red green blue yellow purple
I understand now, that the element "purple" is never evaluated, its skipped when "pink" is removed and the element numbers change. Then the next element "brown" is removed and then there are basically 2 empty spaces that the loop is trying to evaluate.
My classmate made two new arrays and set the loop to move the word to another array, shifting from the @colors and pushing to @new (basically).
For each of you who responded, thank you so much for your help. I will keep using all the resources you have sent so that I can do better in the future.
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.