Loop Iteration Variable.

Discussion in 'Perl Misc' started by Degz, Jan 12, 2006.

  1. Degz

    Degz Guest

    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.
    Degz, Jan 12, 2006
    #1
    1. Advertising

  2. Degz

    Anno Siegel Guest

    Degz <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Jan 12, 2006
    #2
    1. Advertising

  3. Degz

    Anno Siegel Guest

    Degz <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Jan 12, 2006
    #3
  4. Degz wrote:
    > 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
    Jürgen Exner, Jan 12, 2006
    #4
  5. Degz

    Xicheng Guest

    Anno Siegel wrote:
    > 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
    Xicheng, Jan 12, 2006
    #5
  6. Degz

    Anno Siegel Guest

    Xicheng <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Jan 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwerdna
    Replies:
    5
    Views:
    5,402
    Ajeetha
    Mar 31, 2005
  2. Danny Anderson

    open new file each loop iteration

    Danny Anderson, Jan 21, 2004, in forum: C++
    Replies:
    0
    Views:
    440
    Danny Anderson
    Jan 21, 2004
  3. Rudi
    Replies:
    5
    Views:
    5,001
  4. Nene
    Replies:
    6
    Views:
    347
    John W. Krahn
    Dec 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    365
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page