sivasakthi wrote:
Hi all,
Hello,
I have file like that following,
site_name access_time
www.google.com 14:13:04|14:13:04|
172.16.5.49 14:12:10|14:12:56|
172.16.65.53 14:12:41|14:12:58|
172.16.671.35 14:12:29|
from the above file i need to print the sites & no of accessed
connections with sort.
open(FILE,:/tmp/test.txt/" or die "cant open the file");
You have a typo, :/tmp/test.txt/" should be "/tmp/test.txt". Also, you should
include the $! variable in the error message so you know *why* it failed.
open FILE, '<', '/tmp/test.txt' or die "cant open the file: $!";
my @domain_info=<FILE>;
foreach my $site (@domain_info)
{
my ($domain,$data)=split(" ",$site);
$domain{$domain}.=$data;
}
foreach my $dname ( keys %domain)
{
my @conn=split('\|',$domain{$dname});
my $noofconn=$#conn+1;
$noofconns{$dname}+=$noofconn;
}
There is really no good reason to read in the entire file at once:
my %noofconns;
while ( <FILE> ) {
my ( $domain, $data ) = split;
my @conn = split /\|/, $data;
$noofconns{ $domain } += @conn;
}
close FILE;
foreach $dname (sort {$noofconns{$b} <=> $noofconns{$a}} %noofconns)
You forgot to use keys():
foreach my $dname ( sort {$noofconns{$b} <=> $noofconns{$a}} keys %noofconns )
{
print "$domain\n";
print "$noofconns\n";
print "$dname $noofconns{$dname}\n";
}
close (<FILE>);
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/