Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting and print only once

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
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

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";
        }
    }
}
 
Thanks for the help/reply.

Same thing for the most part code below. I prints out the filename 3 times and the count of (1) for each URL.

instead of the filename once and the three links and a count of 3.

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;
        print "$filename\n";
        while (@element) {
            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";
    }
}
 
Sorry, don't quite know the structure of data you are manipulating.
Anyway
[tt]$count = 0;
print "$filename\n";[/tt]
must be before the inner [tt]foreach[/tt] and
[tt]print "$count\n";[/tt]
after the closing brace of the same.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top