Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Help me not use for($i=0;$i=$#array;$i++)...Please!

by IraTarball (Monk)
on Jun 21, 2001 at 01:39 UTC ( [id://90208]=perlquestion: print w/replies, xml ) Need Help??

IraTarball has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently doing
my @array = qw(0 1 2 3 4 5 6 7 8 9 ); for (my $i = 0; $i <= $#array; $i++) { #...process...process...# splice @array, $i, 1 if $array[$i] == 5; #Delete a certain array e +lement. }
and it makes me feel dirty. Does anyone know a way to do this with a regular foreach (@array) type thingy?

Thanks,
Ira

"So... What do all these little arrows mean?"
~unknown

Edit: chipmunk 2001-06-20

Replies are listed 'Best First'.
Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!
by btrott (Parson) on Jun 21, 2001 at 01:48 UTC
    Is that code an example or is that the specific instance you're trying to replace? Because if the latter, you can just use grep:
    my @array = grep $_ != 5, 0..9;
    Actually this isn't exactly the same, because your code has a bug, assuming that your goal is to weed out elements equal to 5. (That's the goal, right? If not, never mind.)

    The bug is that you're altering the array while progressing through the loop, which means that you skip certain elements. Try your code on this array:

    my @array = qw(0 1 2 3 4 5 5 6 7 8 9 );
    Only the first 5 will be removed, because after splice-ing out the first 5, the index of the 5 following it has just decreased by 1. But the loop counter increments, and you end up skipping the 5 following it.

    As for your general question, which seems to be, is there a way to get rid of reliance on loop counters when iterating through arrays. And that's a good question, and I don't really think there is a good way. There isn't a magic variable for "index into an array" when in a foreach loop. Although perhaps there should be; Dominus has a post about this.

    All the same, though, you should probably refrain from altering an array while iterating over it.

      This was an extremely simplified example to try and get to the core problem. In reality this is an array of arrays inside a hash. I'm looping through doing some processing and certain things should only be processed once but others should be processed multiple times. My solution was to remove them from the array after processing if they were to only be processed once.
      After looking at all the great responses here I think @array = grep $_ != 5, @array; is what I want. The 5 would actually be an array reference, but I think this will work.

      Thanks for all the help,
      Ira.

      "So... What do all these little arrows mean?"
      ~unknown

        If you bracket it, the grep expression becomes a block. It can be as complicated as you please. If it evaluates true, $_ (possibly modified within the block) is placed in the returned array.

        After Compline,
        Zaxo

        Update: Corrected block <-> expr terminology.

Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!
by Zaxo (Archbishop) on Jun 21, 2001 at 01:47 UTC
    grep {$_!=5} @array;

    After Compline,
    Zaxo

Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!
by Masem (Monsignor) on Jun 21, 2001 at 01:48 UTC
    If you don't care about what you throw away:
    @array = grep { $_ != 5 } @array;
    If you do care:
    my @thrownaway; @array = grep { if ( $_ == 5 ) { push @thrownaway, $_; 0 } else 1; } @ +array;

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!
by dimmesdale (Friar) on Jun 21, 2001 at 01:50 UTC
    How about:
    for $i(0..$#array) { splice @array, $i, 1 if $array[$i] == 5; }
    Update: Got called away for dinner and didn't get a chance to finish(but it looks like others beat me anyway).

    Also, it all you care about is the value of the array, i.e., not its index, you could use this:

    for (@array) { ... if $_ == 5. }
      I wouldn't use the splice construct inside of a loop iterating over the array. It'll get rid of the right elements, but it'll keep the array the same size due to autovivification at the end. (This may depend on the version of Perl.) Running under strict exposes this:
      my @array = (0 .. 9); $" = " "; for my $i (0 .. $#array) { splice @array, $i, 1 if $array[$i] == 5; print "$i: @array\n"; } print "Array is ", scalar @array, " elements long.\n";
      Bang, uninitialized value and @array is 9 elements long at the end. Yikes.
        And what's worse, if you have consequetive elements equal to 5, only of them is removed. Because the splice moves everything following one down, but the next iteration ups the counter.

        -- Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://90208]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found