Description: Like Array sifting, but the order is kept intact.
sub sift (\@@) {
  my $in = shift;
  my ($i,$j) = (0,0);
  my @s;

  @_ = map ref($_) ? $_ : qr/$_/, @_;

  push @s, map {
    my $ok = 0;
    my $e = $_;
    for my $r (@_) {  
      splice(@$in, $i-$j++, $ok = 1), last if /$r/;
    }
    $i++;
    $ok ? $e : ()
  } @$in;
  
  return @s;
}
 
@orig = qw( where are going to you );
@sifted = sift @orig, qr/^[aeiouy]/, qr/ng$/;
print "@sifted;@orig\n";
# are going you;where to
Replies are listed 'Best First'.
Re: Array sifting, maintaining order
by zentara (Archbishop) on Dec 17, 2001 at 03:34 UTC
    I'm just a novice, so I can't really find the error, but when I run this code I get are going you;to you not are going you;where to
      Thank you for pointing that out. I have corrected the code.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;