in reply to Re^2: Regexp works with some elements and doesn't with others
in thread Regexp works with some elements and doesn't with others

Use an index explicitly instead of the easily broken, implicit one you've coded. It's important to iterate in reverse so your index will still make sense after removing data. That you have these two arrays where elements exist in pairs indicates that your data structure is mismatched. This would be better served by having an array of pairs.

foreach my $ix ( reverse 0 .. $#text ) { if ( not length $text[ $ix ] ) { splice @text, $ix, 1; splice @links, $ix, 1; } }

The previous is optimal on very recent perls and less so on anything older. This is a great reason to use a three-arg for loop.

for ( my $ix = $#text; $ix >= 0; -- $ix ) { # same loop contents. }

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^4: Regexp works with some elements and doesn't with others
by lampros21_7 (Scribe) on Feb 07, 2006 at 02:06 UTC
    Sorry, thats my fault. I put the wrong loop from my file. I have updated it in my previous post now.

      $value is still a terrible name for an index. It's also terrible to code the plumbing of loop indexes manually when you have better alternatives available. That's why the reverse(0 .. $#array) is the preferred syntax.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊