Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: The error says the value is uninitialized, but it works anyway

by mizducky (Novice)
on Aug 18, 2019 at 00:14 UTC ( [id://11104624]=note: print w/replies, xml ) Need Help??


in reply to Re: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

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.

Replies are listed 'Best First'.
Re^3: The error says the value is uninitialized, but it works anyway
by BillKSmith (Monsignor) on Aug 19, 2019 at 15:23 UTC
    Try your experiment with my suggestion for reversing the processing order. (Remember you only edit one line.) Do you see why it runs correctly? The modified program never tries to access an array element which has been moved.
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found