On 9/21/07, sivasakthi <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> 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.
>

To start with, all Perl programs longer than one line should start with

#!/usr/bin/perl

use strict;
use warnings;

>
> open(FILE,:/tmp/test.txt/" or die "cant open the file");

Your use of parenthesis is defeating your use of "or die".  This code
will only die if "/tmp/test.txt" is false (which it cannot be as it is
a non-empty string).  You also have what I hope is a type there at the
start of the file string (: instead of ").  Don't type code into
emails, always copy and paste.  Try doing it like this instead

open my $file, '<', '/tmp/test.txt'
    or die "could not open /tmp/test.txt: $!\n";

>
> my  @domain_info=<FILE>;
>         foreach my $site (@domain_info)

This is the worst way to read a file line by line.  You are wasting
CPU time and memory.  The right way is

while (my $site = <$file>) {

>         {
>               my   ($domain,$data)=split(" ",$site);
>                 $domain{$domain}.=$data;

The data structure you want here is not a hash of strings, but a hash
of arrays.  Try

push @{$domain{$domain}}, $data;

or better yet

push @{$domain{$domain}}, split '|', $data;

>         }
>
> foreach my $dname ( keys %domain)
> {
>         my @conn=split('\|',$domain{$dname});
>         my $noofconn=$#conn+1;
>         $noofconns{$dname}+=$noofconn;
>
> }

This loop should be joined with the one above (see the second example)

> foreach $dname (sort {$noofconns{$b} <=> $noofconns{$a}} %noofconns)

sort takes a list not a hash, you need to say "sort {} keys %hash" instead.

> {
>    print "$domain\n";
>    print "$noofconns\n";

I think this is where your real problem is.  $domain and $noofconns
hold nothing (they aren't even variables under strict).  You should
have said

print "$dname had $noofconns{$dname} connections\n";

> }
> close (<FILE>);
>
> Could u help me to solve the pbm???
>
> Thanks,
> Siva
>
>

Here is how I would have solved the same requirements (if the sort is
too slow then it might be time to try a Schwartzian Transform on it):


#!/usr/bin/perl

use strict;
use warnings;

my %domain;

while (<DATA>) {
        my ($dom, $data) = split ' ';
        push @{$domain{$dom}}, split /\|/, $data;
}

for my $dom (sort { @{$domain{$b}} <=> @{$domain{$a}} } keys %domain) {
        print "$dom had " . @{$domain{$dom}} . " connections\n";
}

__DATA__
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|

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to