in reply to Learning Perl - Question About Using a For Loop To Pull Data From an Array

Well for one, this line $counter = @userNumber; causes the value of $counter to be set to the number of elements in @userNumber. This is the same as $counter = scalar @userNumber;

The foreach loop loops over the contents of @userNumber. Change print $counter; to print "@userNumber\n"; to see the contents of @userNumber instead of the number of things in @userNumber.

Update: looks like some issues here also: @userNumber = <STDIN>; You need a loop here to push the number entered by the user to @userNumber. CTL-D is an EOF. So something like this should work: while (<STDIN>){push @userNumber, $_);}. I didn't run your code, but I think the above will solve a couple of issues. another Update: I did run the code and well @userNumber = <STDIN>; does indeed work. It just looked weird to me as I never use CTL-D (or CTR-Z on Windows) to terminate user input. But a while loop is the right kind of loop because as you write more code, you will want to end the loop on a blank line or some other thing like zero and that condition will go in the while statement. Users don't do well with control characters.

Replies are listed 'Best First'.
Re^2: Learning Perl - Question About Using a For Loop To Pull Data From an Array
by GotToBTru (Prior) on Apr 01, 2016 at 19:40 UTC

    No, you don't need a loop to create the array. The OP indicated that the program worked, only that he did not understand why. The ctrl-D does indicate this should be run in a *nix environment, not a Windows one. I'm with you in that I would never write it this way.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re^2: Learning Perl - Question About Using a For Loop To Pull Data From an Array
by aUserName (Initiate) on Apr 01, 2016 at 18:26 UTC

    Thank you for your reply. As you mentioned the script works fine. This is an example from a textbook which is why it may seem odd and not look like a real world script.

    I am not having any issues with the code, rather I am trying to understand how the for loop is using the $counter variable to pull out the actual number that user entered when it appears that $counter is being used as a scalar.

    For example if the user enters 1 and 3 they get fred and barnet back which are the first and third items in the array respectively.

    What is confusing me is why is $names $counter - 1 giving the correct values back. If the user enters 2 values, such as 1 and 3, $counter would = 2, not 1 or 3 since it is being treated as a scalar. Yet the script correctly returns the 1st and 3rd item in the array instead of the the 1st and 2nd item of the array.

    Thank you for your help.

      It seems that the name chosen, $counter, is confusing you.

      It is being used as an INDEX, rather than as a traditional COUNTER.

      The array is indexed from 0 to (n-1). (Updated to n-1)(Thanks, Corion)

      You need to subtract one from the value entered, in order to get the equivalent index for a zero-based array.

      Hopefully, that clarifies things.

      FYI, Fred's friend is named Barney, not barnet. and Barney's kid is Bamm-Bamm.

              This is not an optical illusion, it just looks like one.

      What this foreach $counter (@userNumber){} does is assign $counter to each value in the array @userNumber. It does this on a first in, first out basis. If the user entered say 1,3,2.. The first loop $counter = 1, then $counter =3, then on last loop, $counter = 2. Before print "\nthe name is: $names[$counter - 1], add a statement, print "counter = $counter\n";. And you can see what counter is. Use some non-sequential test cases.

      Part of the problem here is a rather poor choice of names for "counter". $name_number or something similar would have been better. There is no "count" function of $counter! It is being used as a 1 based index to the names array. Perl starts arrays at 0, not one, so that is what $counter-1 is all about. Hopefully that makes sense?