Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!

by btrott (Parson)
on Jun 21, 2001 at 01:48 UTC ( #90212=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re: Re: Help me not use codefor($i=0;$i=$#array;$i++)/code...Please!
by IraTarball (Monk) on Jun 21, 2001 at 02:13 UTC
    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,

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

      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,

      Update: Corrected block <-> expr terminology.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://90212]
[stevieb]: nice! I just finished a GPS "take me home" device last week, and did a bunch of software updates to it yesterday. I also created a pseudo chip with an Arduino to simulate an IC, where it responds to register read/writes over the I2C bus...
[stevieb]: ...from an I2C master. It's ugly and there are many changes I'm going to make, but I had not done anything like it before. It's designed for my RPi:: automated test platform; a system that does CI on *all* my RPi modules.
[shmem]: pseudo chip?
[stevieb]: well, what happens is the Arduino 'listens' for requests r/w, and does the appropriate thing when it's interrupted based on the 'register' address sent in. It's ugly as it was my first attempt, but I've got great new ideas I'm just sitting.
[stevieb]: ...down to implement now. Here's the sketch as it currently sits
[shmem]: well I use I2C and SPI and stuff, but creating a pseudo chip looks to me like lot of indirection and memory clutter... not?
[choroba]: Are you going to use the device soon? Related to your comment about "not having much time to do a lot of coding"...
[stevieb]: sure, but I'm just learning ;) I consider it practice to get a good understanding of what goes on *after* an I2C/SPI request is made
[shmem]: ah ok. Gonna read that. but now....
shmem compiles himself into his template

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-06-25 22:42 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.