Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm struggling with removing an item from an array. The array is populated with the results of a directory listing, and I am using the array to move the files and do some other stuff. But I have a directory called thumbs that is getting listed too, and it is causing me some problems, so what would be a good way to remove it from the array. This is what I have:
@picturelist = `ls -A $sourcepath`; $p = 1; while ($p <= $#picturelist){ if($picturelist[$p] =~ /thumbs/){splice @picturelist, $p,;} }

Replies are listed 'Best First'.
Re: finding and deleting item from an array
by tachyon (Chancellor) on Jul 01, 2001 at 05:21 UTC

    You want grep. Here is an example:

    @ary = qw(just another perl foo hacker); @no_foo = grep{ $_ ne 'foo' }@ary; print "@no_foo"

    The grep function examines the whole of the @ary array, aliasing each value in turn to $_ within the { }. If the value within the { } is true then that element is added to the list that grep returns. In this case grep returns a list of all the values that are not equal to 'foo'. Thus we have used it to remove one value from the array. Although I have assigned the result to the @no_foo array I could have just as easily assigned it back to @ary. This is just a simple example of what grep can do - it is a really handy function and well worth getting familiar with. Because we alias to $_ you can include a match type regex like /foo|bar/ which will return all the elemnts of the array that match either foo or bar.

    cheers

    tachyon

(ar0n) Re: finding and deleting item from an array
by ar0n (Priest) on Jul 01, 2001 at 05:25 UTC
    Sounds like a job for grep:
    my @pictures = grep not /thumbs/, `ls -A $sourcepath';

    ar0n ]


    update: it seems my time warp machine has failed me again. damn.
Re: finding and deleting item from an array
by srawls (Friar) on Jul 01, 2001 at 05:24 UTC
    You're almose there, just add a one after that comma after $p. The splice would then look like this:
    splice @picturelist, $p, 1;
    The 1 in the above splice is used as a length; it tells how many elements to delete. To learn more about splice, just click one of the multiple links in the above paragraph.

    Update: Well, I was concentrating on your syntax, tachyon provides a much more idiomatic answer; I'd go with his if I were you

    The 15 year old, freshman programmer,
    Stephen Rawls

Re: finding and deleting item from an array
by Zed (Initiate) on Jul 01, 2001 at 09:09 UTC
    Hey thanks guys :) I'm trying to get it to work with the code you suggested, but I am not getting anywhere. I'm sure it's something I am doing wrong, but I am not getting any errors, and it isn't removing thumbs. I am wondering if there is a newline or some whitespace in there causing it not to match? I have tried removing anything else that might be in there but no luck, it could be because I don't know what I'm doing :)) This is what I have thus far:
    @picturetemp = `ls -A $sourcepath`; print @picturetemp; @picturelist = grep{ $_ ne 'thumbs' }@picturetemp;
      My mistake, found I had a problem further down in the code. Thanks again :))