Ramprasad wrote:
> Hello all,
>     I have to remove elements from an array if they match a particualr
> string and put it into another array
> 
> eg if element matches index or default I want it to be in a new array
> 
> I am trying this
> my @arr = qw( 01.html index.html aa.html bb.html cc.html dd.html );
> 
> 
> # the following does not compile
> #map {s/^(index|default).*/{push @new,$_}/e && delete $_  } @arr;
> 
> 
> #This works but it does not remove the array element
> map {s/^(index|default).*/{push @new,$_}/e && undef $_  } @arr;
> 
> print Dumper([\@new,\@arr]);
> 
> I get
>   $VAR1 = [
>            [
>              'index.html'
>            ],
>            [
>              '01.html',
>              undef,
>              'aa.html',
>              'bb.html',
>              'cc.html',
>              'dd.html'
>            ]
>          ];
> 
> the element $arr[1] is undefed but not removed from the array
> 
> 
> Can I delete the element somehow
> I am going to run this for about 60,000 arrays and I cant afford
> needless if's and foreach's 

You want the splice() function.

Here's one approach. I'm sure somebody can come up with one-liner...

    #!/usr/bin/perl

    use strict;

    my @arr = qw( 01.html index.html aa.html bb.html cc.html dd.html );
    my @new;

    my $n = 0;
    while ($n < @arr) {
        push(@new, splice(@arr, $n, 1)), next if $arr[$n] =~
/^(index|default)/;
        $n++;
    }

    print "@arr\n";
    print "@new\n";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to