in reply to Help needed with mechanize
Have a look at How can I extract just the unique elements of an array? (FAQ).
In your case, you could use one of those methods to strip out the duplicates before the foreach loop, or you could integrate a method into the loop itself sort of like this:
my %unique_urls; foreach (@links) { if ($links[$i]->url() =~ m!(-[0-9][0-9][0-9][0-9][0-9][0-9]*)!) { $links[$i]->url() =~ m!([0-9][0-9][0-9][0-9][0-9][0-9]*)!; my $url = $1; $art[$i] = $url; print "$url\n" if ( ! $unique_urls{$url}++ ); } $i++; }
I recommend something like this:
my %unique_urls; my @art = grep { m{-(\d{5,})} && !$unique_urls{$1}++ } map { $_->url() } $agent->links(); undef %unique_urls; print map { "$_\n" } @art;
That may be diverging too much from your original intention, though. It's hard to recommend with confidence without reading the context.
|
|---|