use strict;
use warnings;
use Carp;
#use Net::Random;
use Data::Dumper;

my $data;

open my $in, "<", 'bytes100MB';
binmode $in;
#read $in, $data, 1024;

my $cont = '';
 
while (1) {
    my $success = read $in, $cont, 1024, length($cont);
    croak $! if not defined $success;
    last if not $success;
#		my $bits8 = oct("0b" . (unpack 'b8', $cont));
#		print "$bits8\n";
}
close $in;
print "closed\n";

my $l = length $cont;
#my $l = 10;
my %twofivefive;
my %nn;
#my @bins;
my $offset = 0;

for (1..10) {

	my @bins;
	my $chunk = $offset + int($l/10);

	for (my $i = $offset; $i < $chunk; $i++) {
#	for (my $i = 0; $i < $l; $i++) {
		  my $byte = vec $cont, $i, 8;
	#		$twofivefive{$byte}++;
	#		if ($byte > 99) {
	#			$byte -= 100;
	#			push @bins, $byte if $byte < 100;
	#			$nn{$byte}++ if $byte < 100;
	#		} else {
				push @bins, $byte;
	#			$nn{$byte}++;
	#		}
	#		$twofivefive{$byte}++;
	#		$nn{0}++ if abs($byte-156) == 100;
	#    print $byte, ' ', int $byte*99/255, "\n";
	}

	writefile(\@bins);
	$offset += $chunk;

}

for (sort {$a<=>$b} keys %twofivefive) {
	print "$_ ", $twofivefive{$_}, "\n";
}

for (sort {$a<=>$b} keys %nn) {
	print "$_ ", $nn{$_}, "\n";
}

#writefile(\@bins);

sub writefile {
	my $data = shift;
	my $filename = 'bins100Mb.txt';
#	open(my $fh, '>:encoding(UTF-8)', $filename) or croak "Could not open file '$filename' $!";
	open(my $fh, '>>:encoding(UTF-8)', $filename) or croak "Could not open file '$filename' $!";
	print $fh join("\n", @$data);
	close $fh;
}
  

