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

Part of a script I'm doing right now checks each element (mp3) in the array to see if it has a tag. If it has a tag, then it goes on to do some other actions and so on.

I was wondering if there's any way to simply make it skip that element, if it doesn't have the tag, and go on to the next one in the array. (So that the loop still continues.)

Sorry if this is confusing. Hope someone can help, thanks.

Replies are listed 'Best First'.
Re: Skipping array element in a loop
by davido (Cardinal) on Aug 30, 2006 at 07:03 UTC

    Not confusing at all. What you're asking about is probably next. Here's an example:

    my( @array ) = qw/one two three four five/; foreach my $number ( @array ) { next if $number eq 'three'; print $number, "\n"; }

    The output will be:

    one two four five

    In other words, 'three' is skipped by 'next'. If your loop has a continue{...} block, that portion will still be executed, even if you 'next'.


    Dave

      That's exactly what I needed, thanks for the speedy reply!

      Kevin
Re: Skipping array element in a loop
by Popcorn Dave (Abbot) on Aug 30, 2006 at 18:42 UTC
    To follow up on what davido said, if you're working with ID3V1 tags you can do something like this:

    open FH, "<$mp3" or die "Could not open file $mp3\n"; binmode FH; seek (FH, -128, 2); read (FH, $tag, 128); next if $tag !~ /^TAG/;

    This is using the fact that the ID3V1 tag has the word TAG at the begging of said tag. If you're working with ID3V2 tags, I'd look at using one of the Mp3 modules from CPAN.

    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.