Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

count the number of IP shows up in the file

by sgowrish (Initiate)
on Jan 20, 2009 at 21:44 UTC ( #737693=perlquestion: print w/replies, xml ) Need Help??
sgowrish has asked for the wisdom of the Perl Monks concerning the following question:

Hi Please can someone guide me how to do this ? 1. I have a data file "path.csv" and it's contents is: 2. here basically i need to count the number of times each IP address shows up in the file based on the KEY 3. After loading above KEY:VALUES pair to HASH, i want to read IP and count based on each KEY and print into a file. I have coded something like below,i had given only the function:
sub _Load_IP { if(! open (PATH_FILE_FH, "$Path_File")) { print "Failed to Open input PATH_REPORT File: $Path_File\n"; exit 1; } while(<PATH_FILE_FH>) { my $data =$_; chomp($data); my($pname,@val) = split(/\s+/,$data); my($Starpnt,$Endpnt) = split(/_/,$pname); $key = $Startpnt; #$IP_hash{$key} .= exists $IP_hash{$key} ? map{$IPcountList{$_}++ }" +@val" : @val; $IP_hash{$key} .= "@val"; } close(PATH_FILE_FH); } #End of _Load_IP()

Replies are listed 'Best First'.
Re: Tally up the number of times each IP shows up in the file and load into HASH
by borisz (Canon) on Jan 20, 2009 at 21:57 UTC
    Not sure if I understand what you want to do. But here is my try. < path.csv
    use strict; use warnings; my %h; while (<>) { my ( $k, @ips ) = split /\s+/; $k =~ s/(_.*)$//; $h{$k}{$_}++ for @ips; } for my $srv ( sort keys %h ) { for ( sort keys %{ $h{$srv} } ) { printf( "%10s %15s %6s\n", $srv, $_, $h{$srv}{$_} ); } }
    __OUTPUT__ 11BT801 1 11BT801 2 11BT801 1 11BT801 1 11BT801 2 11BT801 1 11BT801 2 12GT601 1 12GT601 2 12GT601 1 12GT601 1 12GT601 1 12GT601 2 12GT601 1 12GT601 2
      Thank you so much.....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://737693]
Approved by toolic
erix self-flagellates with the cluebat
[erix]: indeed had to be( time make check-world ) > test_run2.output 2>&1
[erix]: probably misremembered because of 2>&1 |
[erix]: oh nice, can do |& nowadays. Learned something new after all
LanX flagelates erix with a signed markov chain

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-03-23 08:55 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (285 votes). Check out past polls.