I found out that I left some unaltered code that I programmed 10 years ago and it went unnoticed. I knew this bug and I actually fixed it quick and dirty in multiple places but I never found a good fix for it.
What is the best way to use the splice inside of a loop like this (foreach, or other)
Original code bugged:
sub filterExclude{ my $ar = $_[0] ; # REF! my $someConfig = $_[1] ; my $ix = 0 ; foreach ( @{$ar} ) { my $sKey = $_ ; if ( exists ( $someConfig ->{ $sKey }->{ Exclude} ) && $someConf +ig ->{ $sKey }->{ Exclude} ) { splice ( @{$ar}, $ix, 1 ) ; $ix = $ix - 1 ; } $ix = $ix + 1 ; } }
Right now I have:
sub filterExclude{ # 20240414 Quick fix needed because of the splice my $ar = $_[0] ; # REF! my $someConfig = $_[1] ; my $tmpCheckedEverything = 0 ; while ( !($tmpCheckedEverything) ) { $tmpCheckedEverything = 1 ; my $ix = 0 ; foreach ( @{$ar} ) { my $sKey = $_ ; if ( exists ( $someConfig ->{ $sKey }->{ Exclude} ) && $someC +onfig ->{ $sKey }->{ Exclude} ) { splice ( @{$ar}, $ix, 1 ) ; $tmpCheckedEverything = 0 ; } $ix = $ix + 1 ; } }
But of course this is ugly and not efficient
In reply to How to use a splice inside a foreach by Veltro
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |