R. Joseph Newton wrote: > "... > foreach(1..$total_elements){ > print "\t\[$_\] my $input[$_ - 1 ]\n"; > } > ...Why did scalar $input in the foreach loop > "works" without predeclaring it with my?" --Eri > > HI Eri, > > The my is probably not doing what you think it is. Otherwise > it would constitute a redeclaration error. My guess is that > you are creating an implicit anonymous scalar here: > print "\t\[$_\] (my $Phantom_scalar = $input[$_ - 1 ])\n"; > You dont need to do this. Perl should interpolate the element > directly.
I don't know if that's what he wants to do, but that definitely is not what's happening. the characters "my" in the string are just characters. Perl isn't doing anything with them. > > An underlying issue here is that you are using foreach to do > the work of for. The for function is designed to work on > indices. Use foreach to work on the elements directly, > without reference to their indices. > my $element; > for (1..$total_elements){ > print "\t\[$_\] my $input[$_ - 1 ]\n"; > } >From perldoc perlsyn, The "foreach" keyword is actually a synonym for the "for" keyword, so you can use "foreach" for readability or "for" for brevity. (Or because the Bourne shell is more famil- iar to you than csh, so writing "for" comes more natu- rally.) Personally, I never use foreach. (Nor csh, for that matter :~) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]