Hello Eso. The first thing I notice in your code is that you loop with the condition $all_the_numbers<20, but $all_the_numbers never changes, meaning you have to exit the loop manually with last (last should mostly be used for early exit, not for the normal stopping condition). The more perlish way to loop a given number of times is:

for (0..$all_the_numbers) { ... # This will be called $all_the_numbers+1 times # (eg, if $all_the_numbers is 1, it will be called for 0 and 1, +so twice) }

About what you are trying to do: it's simpler to avoid modifying an array while you are iterating over it (looping on it's elements). So you should build the second list in a separate array, which you can either do at the same time as the first one, or in a separate loop, like this:

for (0..$all_the_numbers) { my $number = 30 - int(rand(20)); push @all_rand, $number; # Always add to all_rand; next if $number >= 0; # Skip if number is positive push @negative_only; }
Or first build @all_rand, then in a separate loop:
for my $random_number (@all_rand) { push @negative_only if $random_number < 0; }
You still have to declare the variables, and correct the +1 offset on $all_the_numbers.


In reply to Re: Deleting certein elements from an array by Eily
in thread Deleting certein elements from an array by Eso

Title:
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, 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:
    & &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.