Loop Iteration Variable.

D

Degz

Hi,

I am working through an array testing on each element, and on a certain
condition would like to remove the current element.

My Question is, does perl have a built in variable for the current
iteration of the loop, or do I need to add my own counter. ?

My code looks something like this :

my @array=qw\one two three four five\;

foreach (@array ) {
if ( ! -s $_) {
splice(@array,[COUNTER NEEDED HERE], 1);
}

}

Many Thanks For you help
Degz.
 
A

Anno Siegel

Degz said:
Hi,

I am working through an array testing on each element, and on a certain
condition would like to remove the current element.

My Question is, does perl have a built in variable for the current
iteration of the loop, or do I need to add my own counter. ?

My code looks something like this :

my @array=qw\one two three four five\;

foreach (@array ) {
if ( ! -s $_) {
splice(@array,[COUNTER NEEDED HERE], 1);
}

}

No, there isn't such a counter. Even if there was (or you used your
own) the code wouldn't work. Changing the underlying array while
looping over it gives unpredictable results.

Use grep:

@array = grep -s @array;

If you must splice the unwanted elements out of a given list:

! -s $array[ $_] and splice( @array, $_, 1) for 0 .. $#array;

Code untested.

Anno
 
A

Anno Siegel

Degz said:
Hi,

I am working through an array testing on each element, and on a certain
condition would like to remove the current element.

My Question is, does perl have a built in variable for the current
iteration of the loop, or do I need to add my own counter. ?

My code looks something like this :

my @array=qw\one two three four five\;

foreach (@array ) {
if ( ! -s $_) {
splice(@array,[COUNTER NEEDED HERE], 1);
}

}

No, there isn't such a counter. Even if there was (or you used your
own) the code wouldn't work. Changing the underlying array while
looping over it gives unpredictable results.

Use grep:

@array = grep -s, @array;

If you must splice the unwanted elements out of the given array:

! -s $array[ $_] and splice( @array, $_, 1) for 0 .. $#array;

Code untested.

Anno
 
J

Jürgen Exner

Degz said:
I am working through an array testing on each element, and on a
certain condition would like to remove the current element.

My Question is, does perl have a built in variable for the current
iteration of the loop,
No.

or do I need to add my own counter. ?

Why aren't you simply using grep()?

jue
 
X

Xicheng

Anno said:
If you must splice the unwanted elements out of a given list:

! -s $array[ $_] and splice( @array, $_, 1) for 0 .. $#array;
Code untested.

I guess that will mix up the subscripts of the array elements, better
go throught the array from the reversed order, i.e.:

-s $array[$_] or splice( @array, $_, 1) for reverse(0 .. $#array);

Code untested.
Xicheng
 
A

Anno Siegel

Xicheng said:
Anno said:
If you must splice the unwanted elements out of a given list:

! -s $array[ $_] and splice( @array, $_, 1) for 0 .. $#array;
Code untested.

I guess that will mix up the subscripts of the array elements, better
go throught the array from the reversed order, i.e.:

-s $array[$_] or splice( @array, $_, 1) for reverse(0 .. $#array);

You are right, of course. The same rule (start at the end, work backwards)
applies when several substrings of a string are changed successively.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top