ccc31807 wrote:
) This is a newbie question, I admit, but I don't know the answer.
)
) Suppose I am parsing a file line by line, and I want to push to an
) array all substrings on that line that match a pattern. For example,
) consider the listing below. @urls SHOULD contain this: @urls = (http://
) google.com,
http://yahoo.com,
http://amazon.com,
http://ebay.com)
) Instead, it contains only the last value. Using the g modifier doesn't
) help.
)
) I know why @urls contains only the last value, but I don't know how to
) get all the values.
I think you don't actually know why it only contains the last value,
because there are two separate issues with your code.
) Thanks, CC.
)
) -------listing---------------
) use strict;
) use warnings;
)
) my @urls;
) while (<DATA>)
) {
) if (/<a.*href="([^"]+)/) { push @urls, $1; }
) }
First of all, the .* in there will match everything, so in this case it
will match everything from the first <a to the last href="..."
Second, with the /g modifier, the results will not all be put in $1
And third, obviously, this is a lot easier in perl if you realise that it
can do a lot of set processing:
while (<DATA>)
{
push @urls, /<a.*?href="(.*?)"/gi;
}
Or even:
@urls = map { /<a.*?href="(.*?)"/gi } <DATA>
Although that is a lot more memory hungry.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT