Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (9)
    As of 2019-11-12 13:50 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (66 votes). Check out past polls.