C
ccc31807
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.
Thanks, CC.
-------listing---------------
use strict;
use warnings;
my @urls;
while (<DATA>)
{
if (/<a.*href="([^"]+)/) { push @urls, $1; }
}
print @urls;
exit(0);
__DATA__
<html>\n
<body>\n
<h1>My Favorite Sites</h1>\n
<p>\n
My favorite sites are <a href="http://google.com">Google</a>, <a
href="http://yahoo.com">Yahoo</a>, <a href="http://amazon.com">Amazon</
a>, and <a href="http://ebay.com">Ebay</a>.\n
</p>\n
</body>\n
</html>\n
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.
Thanks, CC.
-------listing---------------
use strict;
use warnings;
my @urls;
while (<DATA>)
{
if (/<a.*href="([^"]+)/) { push @urls, $1; }
}
print @urls;
exit(0);
__DATA__
<html>\n
<body>\n
<h1>My Favorite Sites</h1>\n
<p>\n
My favorite sites are <a href="http://google.com">Google</a>, <a
href="http://yahoo.com">Yahoo</a>, <a href="http://amazon.com">Amazon</
a>, and <a href="http://ebay.com">Ebay</a>.\n
</p>\n
</body>\n
</html>\n