imedox
imedox

Reputation: 29

Remove values in an array

This is our array :

my @array = ('?C=N;O=D',
'?C=M;O=A',
'?C=S;O=A',
'?C=D;O=A',
'/lab/blog/wp-content/',
'1-list.txt',
'2014/',
'2015/',
'rbxslider/',
'slideshow-gallery/',
'uigen_2015/'
);

how to remove all values before </lab/blog/wp-content/> value

shift @list until (shift (@list)=~m/wp-contnet/);

i have tried with this way but no result

Upvotes: 0

Views: 45

Answers (4)

ikegami
ikegami

Reputation: 385655

Close.

shift @array until $array[0] =~ /wp-content/;

Or if there's a chance that there might not be anything to remove,

while ($array[0] !~ /wp-content/) { shift @array; }

You might want to make sure you don't loop forever.

while (@array && $array[0] !~ /wp-content/) { shift @array; }

Upvotes: 1

stevieb
stevieb

Reputation: 9296

You're close, when you use the proper array name and correct the typo in "wp-content", but that doesn't include the 'wp-content' element which appears as though you'd like to do. The following checks the first element before shifting, instead of shifting it off and then checking it:

if (grep /wp-content/, @array){
    shift @array until $array[0] =~ /wp-content/;
}

print "$_\n" for @array;

Output:

    /lab/blog/wp-content/
    1-list.txt
    2014/
    2015/
    rbxslider/
    slideshow-gallery/
    uigen_2015/

Upvotes: 0

ysth
ysth

Reputation: 98388

Another way:

@array = grep /wp-content/../(?!)/, @array;

Upvotes: 2

fugu
fugu

Reputation: 6578

my $count = 0;
my @keep;
foreach (@array){
    $count++ if m|/lab/blog/wp-content/|;
    push @keep, $_ if $count > 0;
}
say foreach @keep;

/lab/blog/wp-content/
1-list.txt
2014/
2015/
rbxslider/
slideshow-gallery/
uigen_2015/

Upvotes: 0

Related Questions