For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > Re: summarize bytes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: summarize bytes
Joe Smith

2005-06-07, 8:57 pm

bastardx wrote:
> id34 2005-06-07 323 7211
> id34 2005-06-07 15 2381


That's trivial to do using a hash or two in Perl.

It's the sort of thing found as student exercises
in early chapters of just about any Perl tutorial.

Hint:
$info{$id}{IN} += $in;
$info{$id}{OUT} += $out;

-Joe
Followup-To has been set to comp.lang.perl.misc
Joe Smith

2005-06-07, 8:57 pm

bastardx wrote in <d84idb$r1u$1@nemesis.news.tpi.pl>:

> I wonder if you can point me to some scripts because I don't know perl.
> I think that perl is best for what I want to do.
>
> ID DATE IN OUT
> id12 2005-06-07 12 1091
> id16 2005-06-07 893 17881
> id12 2005-06-07 53 9912
> id34 2005-06-07 323 7211
> id34 2005-06-07 15 2381
> id34 2005-06-07 542 9771
> id16 2005-06-07 90 9912
> id16 2005-06-07 409 6712
> id12 2005-06-07 311 3443
> id34 2005-06-07 342 1123
> id12 2005-06-07 90 7455
> id34 2005-06-07 145 512
> id12 2005-06-07 88 1667
>
> I want to summarize IN and OUT bytes for particular ID per day


Joe Smith wrote:
> That's trivial to do using a hash or two in Perl.


Two hashes: $in{$_} and $out{$_},
One hash: $data{$_}{IN} and $data{$_}{OUT})

#!/usr/bin/perl
use strict; use warnings;
my %data;
$_ = <DATA>; # Swallow first line
$_ = " ID";
$data{$_}{DATE} = 'last DATE';
$data{$_}{IN} = 'TOTAL_IN';
$data{$_}{OUT} = 'TOTAL_OUT';
while(<DATA> ){
my($id,$date,$in,$out)=split;
$data{$id}{DATE} = $date;
$data{$id}{IN} += $in;
$data{$id}{OUT} += $out;
}
printf "%-6s %10s %10s %10s\n",$_,
$data{$_}{DATE},$data{$_}{IN},$data{$_}{
OUT}
foreach sort keys %data;

__DATA__
ID DATE IN OUT
id12 2005-06-03 12 1091
id16 2005-06-04 893 17881
id12 2005-06-04 53 9912
id34 2005-06-04 323 7211
id34 2005-06-04 15 2381
id34 2005-06-04 542 9771
id16 2005-06-04 90 9912
id16 2005-06-05 409 6712
id12 2005-06-05 311 3443
id34 2005-06-06 342 1123
id12 2005-06-06 90 7455
id34 2005-06-07 145 512
id12 2005-06-07 88 1667
bastardx

2005-06-08, 8:58 am


> #!/usr/bin/perl
> use strict; use warnings;
> my %data;
> $_ = <DATA>; # Swallow first line
> $_ = " ID";
> $data{$_}{DATE} = 'last DATE';
> $data{$_}{IN} = 'TOTAL_IN';
> $data{$_}{OUT} = 'TOTAL_OUT';
> while(<DATA> ){
> my($id,$date,$in,$out)=split;
> $data{$id}{DATE} = $date;
> $data{$id}{IN} += $in;
> $data{$id}{OUT} += $out;
> }
> printf "%-6s %10s %10s %10s\n",$_,
> $data{$_}{DATE},$data{$_}{IN},$data{$_}{
OUT}
> foreach sort keys %data;


Thank you very much for that hint
I will try to customize that a litlle bit :)


bastardx

2005-06-08, 8:58 am


> Two hashes: $in{$_} and $out{$_},
> One hash: $data{$_}{IN} and $data{$_}{OUT})
>
> #!/usr/bin/perl
> use strict; use warnings;
> my %data;
> $_ = <DATA>; # Swallow first line
> $_ = " ID";
> $data{$_}{DATE} = 'last DATE';
> $data{$_}{IN} = 'TOTAL_IN';
> $data{$_}{OUT} = 'TOTAL_OUT';
> while(<DATA> ){
> my($id,$date,$in,$out)=split;
> $data{$id}{DATE} = $date;
> $data{$id}{IN} += $in;
> $data{$id}{OUT} += $out;
> }
> printf "%-6s %10s %10s %10s\n",$_,
> $data{$_}{DATE},$data{$_}{IN},$data{$_}{
OUT}
> foreach sort keys %data;
>
> __DATA__
> ID DATE IN OUT
> id12 2005-06-03 12 1091
> id16 2005-06-04 893 17881
> id12 2005-06-04 53 9912
> id34 2005-06-04 323 7211
> id34 2005-06-04 15 2381
> id34 2005-06-04 542 9771
> id16 2005-06-04 90 9912
> id16 2005-06-05 409 6712
> id12 2005-06-05 311 3443
> id34 2005-06-06 342 1123
> id12 2005-06-06 90 7455
> id34 2005-06-07 145 512
> id12 2005-06-07 88 1667



works great
this exacly what i want
how to read this data from file?


Jürgen Exner

2005-06-08, 8:58 am

bastardx wrote:
Someone else wrote:
>
> works great
> this exacly what i want
> how to read this data from file?


Oh pleeeaaasssee: "perldoc -f open"

jue


bastardx

2005-06-08, 3:59 pm


Użytkownik "Jürgen Exner" <jurgenex@hotmail.com> napisał w wiadomości
news:1Mzpe.10324$KQ2.5813@trnddc08...
> bastardx wrote:
> Someone else wrote:
>
> Oh pleeeaaasssee: "perldoc -f open"
>

yes I know :)
I am not a programmer I just want to create some statistc for my dsl router
Open a file is quite easy but then I have to read line by line, get values
separated by space etc etc
If you know any of programming languages it is easy but I dont
This is my first contact with such a problem.
Sorry for any problem.



A. Sinan Unur

2005-06-08, 3:59 pm

"bastardx" <bastardx@S-P-A-Mop.pl> wrote in
news:d86qko$em7$1@nemesis.news.tpi.pl:

> Użytkownik "Jürgen Exner" <jurgenex@hotmail.com> napisał w wiadomości
> news:1Mzpe.10324$KQ2.5813@trnddc08...
> yes I know :)
> I am not a programmer I just want to create some statistc for my dsl
> router


Who cares what you are and what you want to do?

> Open a file is quite easy but then I have to read line by line,
> get values separated by space etc etc


All of which were done for you by Joe Smith. All you had to do was to
replace the DATA handle with a handle you got from open. And you are
still unwilling to look that one simple thing up, and want someone else
to do it.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
bastardx

2005-06-08, 3:59 pm


>
> Who cares what you are and what you want to do?

You :)

ps
Thanks for Leo who send me a correct code.


Joe Smith

2005-06-09, 8:57 pm

bastardx wrote:
> works great
> this exacly what i want
> how to read this data from file?


There are some things that you have to learn on your own.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com