in reply to split post match

grep to verify that each element is not empty:
use strict; use Data::Dumper; my $string = '=123=123=435'; my @array = grep { $_ ne '' } split '=', $string; print Dumper \@array;

You may need to adjust the grep for your needs.

Oh, and in case anyone considers the golfish grep{$_}, remember that this disallows all false values, such as 0 and 0.0.

I am wondering about the first two answers to your post. They match your problem spec closer than my solution. However, mine appears more robust (what happens if the first element isn't the pattern?). Bad data is always to be expected. I think my solution would be slower, but is the robustness enough to offset the that? Depends upon the speed and your needs.

Cheers,
Ovid

Update:

Oops. Abigail is right: '0.0' is true. Good point about the robustness, too. Ugh.

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: split post match
by Abigail (Deacon) on Jun 21, 2001 at 03:40 UTC
    Robustness is in the eye of the beholder. While you don't throw away the first line if the file doesn't start with the pattern, you throw away information if the pattern appears twice in succession. And while it's given that the file starts with the pattern; it isn't given a pattern cannot appear twice. Hence your claim of "more robust" is at best dubious.

    About your remark of grep {$_}, note that the result of split is an array of strings, and "0.0" is true</code>, so the grep {$_} will not filter out the 0.0.

    -- Abigail