my $counter = 0;
foreach my $e ( a .. z ) {
      $counter++;
      if ( $counter == 5 ) {
           ....
      }
}

I know this is a perl idiom but I, and I suspect others, would find a perl
variable useful for the keeping the count when iterating. The draw back
with the above is that $counter has scope outside of the block and that
seems messy. I am not sure why the C style loop, EG:

for (my $i = 0; $i =< $#items; ++$i)
{}

is so unpopular. Probably because it does look nice. It does have the
advantage of keeping all the variables in the immediate block so you don't
need to worry about $counter 100 lines down the code.
A feature request perhaps but I'm sure there are good reasons why the
maintainers haven't added such a perlvar.
Dermot.



On 8 August 2013 18:11, jbiskofski <jbiskof...@gmail.com> wrote:

> my $counter = 0;
> foreach my $e ( a .. z ) {
>       $counter++;
>       if ( $counter == 5 ) {
>            ....
>       }
> }
>
>
> On Thu, Aug 8, 2013 at 12:11 PM, jbiskofski <jbiskof...@gmail.com> wrote:
>
>> my $counter = 0;
>> foreach my $e ( a .. z ) {
>>       $counter++;
>>
>>
>>
>> On Thu, Aug 8, 2013 at 12:05 PM, Unknown User <knowsuperunkn...@gmail.com
>> > wrote:
>>
>>>
>>> Hello,
>>>
>>>
>>> If i am iterating through the elements in an array, at any point is it
>>> possible to say which element i am handling without using a counter? Are
>>> there any builtins that i can use for it?
>>>
>>> ie
>>> foreach my $element (a..z) {
>>>    ...
>>>    if ( i am the 5th element ) { handle me special }
>>>
>>> }
>>>
>>> Thanks,
>>>
>>>
>>
>

Reply via email to