Stuart White wrote: > > I'm working through Beginning Perl, and I'm stuck on a > particular exercise. The exercise asks me to take a > number from <STDIN> and print all the prime numbers > between 2 and that number. (ch 4, pg 145, #3) > > I can take the number from the user and save it in a > scalar. > My solution to listing the numbers between 2 and it, > is to use the range operator on it and 2, and then > store that into an array. > Of course, then all the numbers are squished together. > (How else might I say that?)
I don't know, what do you mean by "squished together"? > So my solution is to > "stringify" the array of numbers by putting the array > inside double quotes and assigning it to itself. That > seems to work. How does that "seem to work"? > However, when I test it by calling print "$list[3]" or > print "@list" I get an error. > "Use of unitialized value in concatenation (.) or > string at primeNumbers.pl line 18, STDIN line 1." > This makes me think that my array is still acting like > a string, which I don't want. > I thought Perl was supposed to convert numbers to > strings and back when needed, right? > Am I correct in my thinking? > > After, assuming that I can get the array into number > form again, I'll work that out later. > > I'll show my code, but, if it's ok with you all, > please don't write code for me, but rather, write an > explanation of why my code is wrong or my thinking > wrong. If you think I'm heading in the wrong > direction, please let me know. Thanks. > > #!/usr/bin/perl > # primeNumbers.pl > use warnings; > use strict; > my $input; > my @list; > my @primelist; > print "I want you to enter a list of numbers bigger > than 2.\n"; > print "To mark the end of your list, enter 0.\n"; > print "Enter a number that is bigger than 2:\t"; > $input = <STDIN>; > chomp($input); > print "This is your number: $input \n"; > @list = (2 .. $input); > print "list first: @list\n"; > @list = "@list"; That is the same as: $list[ 0 ] = join $", @list; Given that $" defaults to ' ' and assuming @list contains ( 2, 3, 4, 5, 6 ) you will have the string '2 3 4 5 6' in $list[0] and the rest of the array will be empty. > print "list second: @list\n"; > print "this is this the fourth element: $list[3]\n"; That will give you the warning "Use of unitialized value in concatenation" because $list[3] is now undefined. > #foreach (@list) > #{ > # if ($_ % 2 == 0) > # { > # unshift(@primelist, $_); > # print "this is the inloop primelist: @primelist\n"; > # } > #} > #print "this is the outloop primelist: @primelist\n"; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>