in reply to Re: matching every occurrence of a regex
in thread matching every occurrence of a regex
That's a good and very natural approach to the problem. What most other posters here didn't consider is that the whole sequence is stored in a string. This string might be read in from a file but doesn't have to be, so all these while (<FILE>) {...} approaches might not work.
However - especially if the string is long - your double splitting might be doing a lot of unnecessary extra work. First creating an array by splitting on '\n', then a second array by splitting each line and then slicing this array.
If the data is really as simple and consistent as is given in the example, this will do as well with much less effort
my $data = <<STR; BC001593 91 NPSL BC001593 262 NASS BC001593 293 NAST STR my @nums = $data =~ /\s(\d+)\s/g; print join(', ', @nums), $/;
If the real string differs from the given example and the lines contain e.g. more 'whitespace surrounded numbers', then the regex has to be massaged accordingly. Nevertheless the general idea should still work.
-- Hofmator
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: matching every occurrence of a regex
by ihb (Deacon) on Jan 07, 2003 at 16:44 UTC | |
by Hofmator (Curate) on Jan 07, 2003 at 17:13 UTC | |
by ihb (Deacon) on Jan 08, 2003 at 23:21 UTC | |
by IlyaM (Parson) on Jan 09, 2003 at 10:24 UTC |