foreach loop current index

2005-05-20 Thread Peter Rabbitson
Hello, When perl executes a foreach loop, it creates some kind of array of all the iterators and then goes over them one by one. Is the current index pointer accessible from inside the loop? I was unable to find anything related in perlvar. Thanks for the input Peter P.S. I know it is trivi

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > When perl executes a foreach loop, it creates some kind of array > of all the iterators and then goes over them one by one. Is the > current index pointer accessible from inside the loop? I was > unable to find anything related in perlvar. I think you

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
>foreach $item ( @list ) { > sub( $item ); >} > Are you sure this is not a typo? I've never seen a sub statement taking a list argument... Besides this is what I get if I run it anyway: [EMAIL PROTECTED]:~$ cat 123 @list = qw(a bc c); foreach $item ( @list ) { sub( $item );

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > >foreach $item ( @list ) { > > sub( $item ); > >} > > > > Are you sure this is not a typo? Sorry, I didn't mean that literally, I was just trying to stub in an indicator for "do something useful with $item". In hindsight, foreach $i

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
> When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in > perlvar. I think I should clarify myself a little. Here is an

Re: foreach loop current index

2005-05-20 Thread Chris Devers
On Fri, 20 May 2005, Peter Rabbitson wrote: > my @lsit = qw(a b c d); > my %indexes; > > my $current_index = 0; > > foreach my $element (@list) { > $indexes{$element} = $current_index; > ++ $current_index; > } Ah. Maybe this? for (0 .. $#list) { $indexes{$list[$_]} = $_;

Re: foreach loop current index

2005-05-20 Thread Peter Rabbitson
On Sat, May 21, 2005 at 12:11:42AM -0400, Chris Devers wrote: > On Fri, 20 May 2005, Peter Rabbitson wrote: > > > my @lsit = qw(a b c d); > > my %indexes; > > > > my $current_index = 0; > > > > foreach my $element (@list) { > > $indexes{$element} = $current_index; > > ++ $current_index; >

Re: foreach loop current index

2005-05-21 Thread John Doe
Am Samstag, 21. Mai 2005 04.21 schrieb Peter Rabbitson: > Hello, > > When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in >

Re: foreach loop current index

2005-05-21 Thread Paul Johnson
On Fri, May 20, 2005 at 09:21:04PM -0500, Peter Rabbitson wrote: > When perl executes a foreach loop, it creates some kind of array of all the > iterators and then goes over them one by one. Is the current index pointer > accessible from inside the loop? I was unable to find anything related in

Re: foreach loop current index

2005-05-21 Thread Offer Kaye
On 5/21/05, Peter Rabbitson wrote: > Hello, > > When perl executes a foreach loop, it creates some kind of array of all the > iterators I'm assuming you mean that this code: foreach my $num (1..1_000_000) { do_something($num); } creates a one-million number array? No, it doesn't, at least not

RE: foreach loop current index

2005-05-21 Thread Charles K. Clarkson
Offer Kaye wrote: : : Neither was I, but I suspect the reason is that if you need a : counter, you should not be using "foreach". Instead, use "for": : for(my $counter = 0; $counter < @array; ++$counter) { : do_something_with($array[$counter]); : } Why is that for

Re: foreach loop current index

2005-05-21 Thread JupiterHost.Net
: Neither was I, but I suspect the reason is that if you need a : counter, you should not be using "foreach". Instead, use "for": : for(my $counter = 0; $counter < @array; ++$counter) { : do_something_with($array[$counter]); : } Why is that form favored over this typical foreach form? fore

Re: foreach loop current index

2005-05-21 Thread Peter Rabbitson
> The reason this has not been built into the language is that there would > be an overhead associated with each loop to maintain this variable. The > Perl5 team felt that this was not acceptable given that most loops do > not need this information and, and you have noted, it's trivial to do > the