Opening and closing files:
Open a file with open(); function. I recommend something like this :
I would be nice if you checked out what "IN", "<" stand for :)#!/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;
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:#!/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 }
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:www.1 5 www.2 7 www.3 2 ...
Ok we mastered hashes, now let put all this together:#hey, what is my current count for www.2 ? my $value = $hash{"www.2"}; print "$value\n"; # What does "\n" stand for?
I believe this is enough to get you started :)#!/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 :))? }
Cheers
baxy
In reply to Re: Perl File Handling
by baxy77bax
in thread Perl File Handling
by ahyman1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |