Ok - I feel real stupid asking something as dumb as this but hey I'm a complete newbie... Let's say I've got an array and I'm using a foreach loop to divide a number by all the numbers in the array and each time I get $number % $number_from_array == 0 I want to add that number to the end of the array. Somehow this leads to the array being mixed up a bit when running through the foreach loop. This is the code I'm using:
print "Enter a limit\n"; $limit = <STDIN>; @primes = (2); $number = 2; $something = 1; while ($number < $limit) { $something = 1; foreach $div (@primes) { print "Dividing $number by $div\n"; if ($number % $div == 0) { print "$number ist no Primnumber\n"; $number++; $something = 0; } } if ($something == 1) { print "$zahl is no prime number\n"; push (@primes, $number); $number++; } }
Yeah... you gessed right - I'm trying to make a program that works out prime number up to a certain limit... I know this is really inefficient, weird, etc. but I've only started programming. Thanks for your help in advance, Martin

In reply to Newbie Question on pushing Arrays by m_heimburger

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.