in reply to Re^3: Perl program - I hope I can more understand my code
in thread Perl program - I hope I can more understand my code

Thanks so much. I am trying to rewrite this program by using the foreach and push function. I think I am not right, can hint me?
#!/usr/bin/perl -w use strict; my @primes = (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47); sub listprimes { my $n = shift; my $i = 0; my $answer = ""; foreach $x ($primes[$i]<=$n) { push $x{$n} } sub random { my($a,$b) = @_; return int(rand($b-$a+1))+$a; } my $a = random(10,50); my $f = listprimes($a); print "$f\n";

Replies are listed 'Best First'.
Re^5: Perl program - I hope I can more understand my code
by AppleFritter (Vicar) on Oct 22, 2015 at 19:13 UTC

    You're on the right path, but you're not using foreach and push the way they're intended to yet. If you've not done so yet, I'd suggest starting by reading both Compound Statements (for foreach) and push (for, well, push).

    However, I realize that these documents are aimed at people who are already familiar with programming in principle, so here's some extra hints. First of all, let's deal with foreach the way it's intended to be. You have:

    foreach $x ($primes[$i]<=$n) {

    But what does foreach actually do? It iterates through a list of values, assigning each to the loop variable in order and then executing the loop body. Your code, however, does not have a list of values, it has a boolean condition ($primes[$i]<=$n) that might evaluate to true or false. That's good for a while loop, but won't do for a foreach loop.

    Think back to what you're trying to achieve: you want to list primes up to a certain maximum. So what list should you iterate through? The prime numbers, which you already have in @primes.

    Now, the loop body:

    push $x{$n}

    What does push do? It appends a new element to an array, at the end. As such you'll need two things: an array to append to, and an element to append. Again, think about what you want to obtain: a list of primes that meet a certain condition. So you'll need a new array that will hold this list; and the element you'll need to push is the prime currently being considered, if it does indeed meet the condition.

    I don't want to solve your entire homework for you, but here's some pseudo-code to show you what you need to do:

    foreach loop to iterate through the primes { if prime number currently being considered does not exceed $n { push it to the result list. } }

    I hope this'll help.

    Two more notes: first, you will have to declare your result list before using it, using my, just like all the other variables. And second, you will also have to declare your loop variable ($x in your code), though Perl allows you to do this in the loop (foreach my $x (...) {) .

Re^5: Perl program - I hope I can more understand my code
by AnomalousMonk (Archbishop) on Oct 22, 2015 at 22:20 UTC

    If my memory serves me correctly, you have changed the code in the OP (without citation; please see below). The latest code I see in the OP seems very close to what I think you want; it certainly seems closer than what I see above.

    If you compile the latest code in the OP you see error messages like
        Global symbol "@prime" requires explicit package name at ...
        Missing right curly or square bracket at ...
    (The ... represent line numbers that I do not see because I am compiling from the command line. If you compile from a source file, you should see valid line numbers.) Please address these (fairly minor) errors in the latest code in the OP.

    Once you correct these errors, please consider the statement
        my $f = listprimes($a);
    This takes the list returned from the  listprimes() call and assigns it to a scalar variable. Is this correct? What would happen if this list was assigned to an array and the array was then printed? If you address this final problem, I think you're done.

    Finally, if my memory serves me correctly, you have changed the code in the OP without citation! Please don't do that; it makes every reply that refers to the original Original Post inconsistent and incoherent. Please see How do I change/delete my post? for the proper etiquette for making changes to your posts. The basic commandment is this: Do Not Destroy Context.

    Also: don't use  $a $b for lexical variable names; they are Perl special variables (see perlvar) and have special uses.


    Give a man a fish:  <%-{-{-{-<

      Finally, if my memory serves me correctly, you have changed the code in the OP without citation! Please don't do this; it makes every reply that refers to the original Original Post inconsistent and incoherent. Please see How do I change/delete my post? for the proper etiquette for making changes to your posts. (The basic commandment is this: Do Not Destroy Context.)

      Indeed. I've Considered the OP and put its original content (after code tags were added) on my scratchpad so that a janitor may restore it.