Here is what I am trying to accomplish:
1. Grab a index.html page
2. Parse out all of the URLs
3. Create a unique list of the URLs
4. Convert the unique list of URLs to a unique list of IP addresses.
So far I have 3 1/2 of this done. I am able to print out a unique list of URLs (stored in an array) - I can convert URLs to IP addresses but somehow I am loosing the unique portion of the equation when I print out the resolved urls.
I have never done any programming before - so I suspect it may be syntax related but I can't seem to find the problem. I have read through the docs on the modules I am using but still had some lingering questions. I posted at Perl Guru forums but the repose I got was - ask the monks!
Line 62 is a comment explaining the closest I have come and following on 64-67 is what prints out the full list of IPs (but with duplicates).
In the comments you can see I made notes of where I can prove the array is returning unique elements. (I tested by printing the array).
I may have made a bad assumption however. When I start using the Net::DNS module to resolve the URLs I do the following:
My assumptions are:my $res = Net::DNS::Resolver->new; foreach $urls (@hrefs) { my $query = $res->query($urls, "A");
a) $urls will inherit only the unique elements of the @hrefs array
b) that I can only query a scalar variable...not an array using Net::DNS.
Any help would be greatly appreciated!
Cheers!
Here is the complete script:
#!/usr/bin/perl use LWP::Simple; use Array::Unique; tie @hrefs, 'Array::Unique'; tie @ips, 'Array::Unique'; use Net::DNS; # Store the output of the web page (html and all) in the $content vari +able # print "URL to Parse? "; $a = <STDIN>; chop $a; $b = "http://"; $c = $b . $a; $" = \n; my $content = get("$c"); if (defined $content) { #$content will contain the html associated with the url mentioned abov +e. #print $content.""; } else { #If an error occurs then $content will not be defined. print "Error: Get failed"; } # # Parse ALL URLs from the .html page and store all unique entries in t +he @hrefs array # $content =~ s/\'//g; $content =~ s/\+//g; @hrefs = ($content =~ m/:\/\/\"*([\w\-]+\.[\w\-\.]+?)[\"|\/|\s+]/ig); # # Resolve the URLs stored in the array # my $res = Net::DNS::Resolver->new; foreach $urls (@hrefs) { # ## things are unique here!! # my $query = $res->query($urls, "A"); # ## things are unique here!! # if ($query) { foreach $rr (grep { $_->type eq 'A' } $query->answer) { # ## At this location uniqueness is not preserved # # print ($rr->address . ' '); # This prints out many duplicate IP + addresses in a scalar variable # # This was one of my attempts to get things working # This is the closest to what I want to do but the list is not unique +. # @ips = ($rr->address); foreach $uips (@ips) { print $uips . "\n"; } } } else { warn "query failed: ", $res->errorstring, "\n"; } } # # for troubleshooting - this print statement is unique # #print $urls . "\n"; # ## More testing below - didn't work # # # Add unique IPs to array @ips # #foreach $unique (@ips) { # print "@ips"; # print $unique . "\n"; #}
In reply to issues maintaining uniqueness by jkstraw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |