perlnewbie9292
Programmer
Hello all,
I am pretty new to Perl and still learning my way around the syntax and commands. I've been trying to figure out how to do the following for the last few days and not getting anywhere.
What I am having trouble with specifically is:
I am trying to print the filename then below the links that are found, below that the total number of links that were found in that file.
The problem what I have right now if that the count prints out a bunch of times along with the filename.
So if I have two files each with three links the filename prints out the filename a bunch of times along with the count for that line.
When there is a link the count shows up as 1 which is correct, but I just want to print the filename once along with all the links then the total count of links
So for example a two files each with 3 links should be something like
Thanks for the help in advanced
I am pretty new to Perl and still learning my way around the syntax and commands. I've been trying to figure out how to do the following for the last few days and not getting anywhere.
What I am having trouble with specifically is:
I am trying to print the filename then below the links that are found, below that the total number of links that were found in that file.
The problem what I have right now if that the count prints out a bunch of times along with the filename.
So if I have two files each with three links the filename prints out the filename a bunch of times along with the count for that line.
When there is a link the count shows up as 1 which is correct, but I just want to print the filename once along with all the links then the total count of links
So for example a two files each with 3 links should be something like
Code:
filename
found_link1
found_link3
found_link2
3
filename2
found_link1
found_link3
found_link2
3
Thanks for the help in advanced
Code:
#!/usr/bin/perl
use warnings;
use HTML::LinkExtor;
my @Files = `find /home/smahr/[URL unfurl="true"]www_test[/URL] |grep -e \.html\$`;
chomp @Files;
foreach my $filename (@Files) {
$parser = HTML::LinkExtor->new(undef, $filename);
$parser->parse_file($filename);
@links = $parser->links;
foreach $linkarray (@links) {
my @element = @$linkarray;
my $elt_type = shift @element;
$count = 0;
while (@element) {
print "$filename\n";
my ($attr_name , $attr_value) = splice(@element, 0, 2);
if ($elt_type eq 'a' && $attr_name eq 'href') {
print "\t$attr_value\n" if $attr_value->scheme =~ /http|https/gi;
$count++ if $attr_value->scheme =~ /http|https/gi;
}
print "$count\n";
}
}
}