M
Marek
Hello all!
Once again thank you for all for your help with my "rolling frame" in
the thread "restrict a hash to 15 pairs and iterate over it" some
weeks ago! I learned a lot of all your suggestions!
I need once more your help in creating a multi-level hash like
follows:
We read in the line numbers, we count the lines ***and*** I need to
know, how many times these numbers occur in a frame of five lines ...
So my hash should have these informations in pseudo Perl code:
number @(line, line, line) => how many times (each number)
How to transform the genius code of the suggestion from Tad J
McClellan that the hash creation
$nums{$_}++ for values
%lines;
contains meantime the line numbers?
here the example code and best greetings to all
marek
#!/usr/bin/perl
use warnings;
use strict;
use Data:umper;
my $size = 5; # 5 instead of 15
my $line = 0; # "line" counter
my %lines; # buffer up $size lines
while ( <DATA> ) {
next if /\./; # skip dates
chomp;
$line++;
$lines{$line} = $_;
if ( keys %lines == $size ) {
print Dumper \%lines; # for debugging
# count what is in the buffer
my %nums;
$nums{$_}++ for values %lines;
# display what is in the (counted) buffer
foreach my $num ( sort { $a <=> $b } keys %nums ) {
printf "%3d: %3d times\n", $num, $nums{$num};
}
print "---------\n";
# maintain buffer size
delete $lines{ $line - $size + 1};
}
}
__DATA__
01.01.98
7
31
33
14
7
7
35
16
20
20
13
55
1
1
7
7
9
20
21
20
7
20
Once again thank you for all for your help with my "rolling frame" in
the thread "restrict a hash to 15 pairs and iterate over it" some
weeks ago! I learned a lot of all your suggestions!
I need once more your help in creating a multi-level hash like
follows:
We read in the line numbers, we count the lines ***and*** I need to
know, how many times these numbers occur in a frame of five lines ...
So my hash should have these informations in pseudo Perl code:
number @(line, line, line) => how many times (each number)
How to transform the genius code of the suggestion from Tad J
McClellan that the hash creation
$nums{$_}++ for values
%lines;
contains meantime the line numbers?
here the example code and best greetings to all
marek
#!/usr/bin/perl
use warnings;
use strict;
use Data:umper;
my $size = 5; # 5 instead of 15
my $line = 0; # "line" counter
my %lines; # buffer up $size lines
while ( <DATA> ) {
next if /\./; # skip dates
chomp;
$line++;
$lines{$line} = $_;
if ( keys %lines == $size ) {
print Dumper \%lines; # for debugging
# count what is in the buffer
my %nums;
$nums{$_}++ for values %lines;
# display what is in the (counted) buffer
foreach my $num ( sort { $a <=> $b } keys %nums ) {
printf "%3d: %3d times\n", $num, $nums{$num};
}
print "---------\n";
# maintain buffer size
delete $lines{ $line - $size + 1};
}
}
__DATA__
01.01.98
7
31
33
14
7
7
35
16
20
20
13
55
1
1
7
7
9
20
21
20
7
20