I've been running through examples in the PERL textbook. This example asks me to find all the prime numbers up to the number I enter via STDIN. The code looks like this. it will print "1 2 35791113151719" if my stdIN is 21. How do I make sure that there is a space between each number? The book doesn't address this.

#! /usr/bin/perl #prime_number.pl use warnings; use strict; print "To what number would you like to calculate the primes?", "\n"; my $n = <>; my $i = 3; print "1 2 "; #keep executing search until the num ($i) reaches given value ($n) OUTER: while ($i <= $n) { #for each iteration, begin division by 2 my $num = 2; #check to see if value is prime for (1 .. $i){ if (($i % $num == 0) and ($i != $num)) { #if not, check next num $i++; next OUTER; } if ($num > sqrt($i)) { print "$i"; $i++; next OUTER; } $num++; } }

In reply to Spaces between outputs. by CPTQuesoXI

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.