Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by haukex (Chancellor)
on Aug 17, 2019 at 09:48 UTC ( #11104609=note: print w/replies, xml ) Need Help??


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

Hello and Welcome to Perl and the Monastery, mizducky!

You are storing the length of the @colors array in the variable $count, but then modifying the length of the array by using splice to remove elements. So, because you are reducing the length of the array from 7 down to 5, and you are still trying to access elements from the original length of the array, which now no longer exist, you're getting the warning "Use of uninitialized value in string eq".

By the way, the more Perlish way to iterate over the indicies of an array is for my $num (0..$#colors), where $#colors returns the index of the last element of the array.

Note that your code suffers from an issue: If the two colors to be removed are immediately after one another in @colors, only one will be removed. Can you see why that is? If you're not sure, see the Basic debugging checklist: something as simple as printing the values of the variables as your loop runs will already give you a lot of information.

Personally, when I have to modify an array I am iterating over, I like to use a classic for (my $i=0; $i<@array; $i++) loop, because if I am inserting or removing elements, that form gives me the most control over $i, which I may have to modify depending on where I'm inserting/removing elements. If that gets too complicated, note that in Perl, There Is More Than One Way To Do It. Unless your homework assignment explicitly states that you should iterate over the looparray this way, here are some ideas:

  • You could iterate over the elements of the @colors array, pushing the ones you want onto a new array.
  • You could use grep to filter the @colors array.

Another limitation of your code is that it only works for the case of @drop having two elements - this could also be solved in a few different ways:

  • You could loop over the @drop array as well, but that will become inefficient as the arrays grow.
  • You could store the values of the @drop elements as the keys of a hash, this will be much more efficient since hash lookups are fairly cheap.
  • A more advanced technique, which is more applicable if the things you're looking for aren't fixed strings, would be to build a regular expression from the elements of @drop, as described in the tutorial Building Regex Alternations Dynamically.

Replies are listed 'Best First'.
Re^2: The error says the value is uninitialized, but it works anyway
by mizducky (Novice) on Aug 17, 2019 at 09:56 UTC
    The homework question just defines @colors and @drop and says to remove the things in drop from colors, that's it.
    The code I posted does print @colors with out the items from @drop, but since I'm just learning this for the first time that's the only way I could think of to do it.
    I'll keep playing with the things you suggested. Thank you for your help!
      The homework question just defines @colors and @drop and says to remove the things in drop from colors, that's it.

      In that case, the "easiest" way to go about it would probably be to build a hash from the @drop array, and to use grep to filter the @colors array. I could write these two lines of code for you, but since this is homework (thanks for letting us know, by the way), it's probably best if you see if you can figure it out :-) But feel free to ask any questions about it here, or ask if you get stuck!

      Hint: See "Data: Arrays" in perlfaq4 (just ignore the stuff about ~~, that's still experimental).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2019-12-09 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?