Ok, since you look like you are starting with Perl and maybe missed a couple of classes ;) I'll give you a couple of pointers :)

Opening and closing files:

Open a file with  open(); function. I recommend something like this :

#!/usr/bin/perl use strict; use warnings my $file = "Name.txt"; open(IN, "<", $file) or die "I cannot open a file: $file "; # close file: close IN;
I would be nice if you checked out what "IN", "<" stand for :)
Once you have opened the file you are ready for reading it. Please thank all mighty Perl for being able to read files line by line by default. To do that all you need to do is to loop through a file with a while loop. Now, how will you know where your file is?. Well, by opening a file you placed your file handle into "IN" variable. (I am trying to make this as simple as possible to understand for someone not familiar with programming, therefore you can read my previous sentence as : put my file in "IN"). once you have done that go through it:
#!/usr/bin/perl use strict; use warnings my $file = "Name.txt" open(IN, "<", $file) or die "I cannot open a file: $file "; while(my $line =<IN>){ ## reading a file line by line # here you need to put some regular expression to identify your web +addresses }
Ok, how do we compute percentage. well we count the total amount of www's and the number of each one separately and divide each by the total. Here it would be nice to have some kind of table like this one:
www.1 5 www.2 7 www.3 2 ...
Now again, Thank you Perl for hash tables!!! Hash tables are very simple, you have a key (the first column) and a value (second column) and every time you ask for a www address all you need to do is something like this:
#hey, what is my current count for www.2 ? my $value = $hash{"www.2"}; print "$value\n"; # What does "\n" stand for?
Ok we mastered hashes, now let put all this together:
#!/usr/bin/perl use strict; use warnings my $file = "Name.txt"; open(IN, "<", $file) or die "I cannot open a file: $file "; my %hash = (); while(my $line =<IN>){ ## reading a file line by line # here you need to put some regular expression to identify your web +addresses # from regular expression extract the WWW address and put it into so +me variable like my $www = $1; #hey, what is my current count for some www ? my $value = $hash{$www}; #OK, cool add one to it :) $value = $value + 1; #Good, now put it back. $hash{$www} = $value; } #close file close IN; # Print my averages foreach my $key (keys %hash){ print "$key\t" . (($hash{$key}/$total)*100) . "\%\n"; # hey where di +d my $total come from (I'll leave that to you :))? }
I believe this is enough to get you started :)

Cheers

baxy


In reply to Re: Perl File Handling by baxy77bax
in thread Perl File Handling by ahyman1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.