in reply to Re: Rebinding closures
in thread Rebinding closures

I wanted to be able to access @rules and @replacements by index.

Replies are listed 'Best First'.
Re: Re: Re: Rebinding closures
by duff (Parson) on Dec 17, 2003 at 01:50 UTC

    I saw that but the only place you did it was right at the top of your loop. If you had accessed the rules/replacements by index in more places, it wouldn't have even occurred to me to ask or post an alternative.

      I suppose I could have written that without looping twice over grep. I had this thought of verifying that all the rules matched something since it might be considered an error condition to attempt to replace lexicals that don't exist. Then again, in a forest of indexed lists (PADLIST zero is the names, >=one is the pads, on each component of PADLIST only the index 1+ is interesting, etc) the whole thought is on doing everything indexed. The only remaining problem is that there is no guarantee that your two shifts execute in any particular order since you didn't separate the two modifications to @_ into two statements. You might get the replacement first and then the rule.

      my @replacements; my @rules; for ( 0 .. $#replacement_pairs ) { push @{ $_ & 1 ? \@replacements : \@rules }, $replacement_pairs[$_]; }