QUOTE(enigma-paradox @ 17 Jun, 2008 - 06:15 PM)

there are a few questions that I have. Like I said I'm trying to teach my self perl.
1. I what is the diffeence between
@key= (); and my @key=(); ?
2. I know how a regular for loop works, but why is a for loop different in perl and what do the differences mean?
3. What does STDIN mean?
4.Where did $q and $r come from? I can't really tell what these varialbes are doing.
push @answers, $r; is what I wanted to know originally. This is what you used so that you only enter a specified number of elements in your code right? That was my original question.
5. Also what's wrong with my perl code exactly? I know that it works in java.
also, are there any books or websites that anyone would recomend for learning perl?
1. My code uses the "strict" pragma, which forces you to declare almost all of your variables with the keyword "my". Nearly all perl scripts should use "strict", and it is especially important for beginners as it catches many errors that are hard to spot otherwise.
2. You were using C style loops: for($i=0; $i<$questions;$i++)
In perl you can use a range to create a loop of n numbers: for (1 .. $questions)
There is nothing "wrong" with C style loops. The two style of loops work the same although the way I coded the loop is probably more efficient.
3. STDIN is user input from the console and other sources of input into a perl program. Its basically a filehandle.
4. $q and $r are just variables to store the value of the user intput. They can be named anything.
5. The obvious answer is that your code does not work, probably just because of using "pop" instead of "push" but the coding style is also old. I'm not sure what your learning material is but if it shows to use this syntax: @key[$i] it is very old. Perl 5.xx still supports that old style syntax to access the elements of arrays but it will generate a warning (if warnings are turned on and they should be).
You should also not wrap print commands in parenthesis:
CODE
print ("blah blah");
should be:
CODE
print "blah blah";
or really if there are no variables or meta characters:
CODE
print 'blah blah';
See here for more details:
http://perldoc.perl.org/functions/print.htmlThe do {} block at the end of the code is not really needed but it does no harm to have it in.
A good place to start with perl is the "tutorial" section of the perlmonks website:
http://www.perlmonks.com/index.pl?node=TutorialsAlso
http://perldoc.perl.org/