How would one calculate binary statistics in perl?












0














I am essentially seeking to do what a typical/good hex editor can do:



https://www.hhdsoftware.com/doc/hex-editor/statistics-statistics-tool-window.html



I wish to be able to count the occurrence of each byte and put it into a table so I can determine the % of say '00' compared to 'FF'.



I have managed to get entropy, and the other statistics such as mean, median and mode are kind of redundant once I have the above complete.



There is also an issue that the binary files I am compiling statistics on are quite large, 32mb+.



Any suggestions?










share|improve this question



























    0














    I am essentially seeking to do what a typical/good hex editor can do:



    https://www.hhdsoftware.com/doc/hex-editor/statistics-statistics-tool-window.html



    I wish to be able to count the occurrence of each byte and put it into a table so I can determine the % of say '00' compared to 'FF'.



    I have managed to get entropy, and the other statistics such as mean, median and mode are kind of redundant once I have the above complete.



    There is also an issue that the binary files I am compiling statistics on are quite large, 32mb+.



    Any suggestions?










    share|improve this question

























      0












      0








      0







      I am essentially seeking to do what a typical/good hex editor can do:



      https://www.hhdsoftware.com/doc/hex-editor/statistics-statistics-tool-window.html



      I wish to be able to count the occurrence of each byte and put it into a table so I can determine the % of say '00' compared to 'FF'.



      I have managed to get entropy, and the other statistics such as mean, median and mode are kind of redundant once I have the above complete.



      There is also an issue that the binary files I am compiling statistics on are quite large, 32mb+.



      Any suggestions?










      share|improve this question













      I am essentially seeking to do what a typical/good hex editor can do:



      https://www.hhdsoftware.com/doc/hex-editor/statistics-statistics-tool-window.html



      I wish to be able to count the occurrence of each byte and put it into a table so I can determine the % of say '00' compared to 'FF'.



      I have managed to get entropy, and the other statistics such as mean, median and mode are kind of redundant once I have the above complete.



      There is also an issue that the binary files I am compiling statistics on are quite large, 32mb+.



      Any suggestions?







      perl binary statistics counting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 at 5:03









      BwE

      187




      187
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Heres another way to do it:



          use strict;
          use warnings;
          use Time::HiRes qw( time );

          $/ = 1;

          open my $file, '<', shift;
          binmode $file;

          my %seen;

          my $start = time();
          my $n;

          while (<$file>) {
          $seen{$_} ++;
          $n++;
          }
          my $end = time();

          for ( sort keys %seen ) {
          printf( "%s%s%.2f%sn", uc( unpack( 'H*', $_ ) ), " seen $seen{$_} times - ", $seen{$_} / $n * 100, "%" );
          }

          printf( "took %.3f seconds!n", $end - $start );


          output:



          ...
          ...
          F8 seen 46475 times - 0.28%
          F9 seen 46611 times - 0.28%
          FA seen 46703 times - 0.28%
          FB seen 48902 times - 0.29%
          FC seen 46829 times - 0.28%
          FD seen 47707 times - 0.28%
          FE seen 47276 times - 0.28%
          FF seen 1752333 times - 10.44%
          took 2.374 seconds!


          This is (WSL in windows) perl 5.22.1 built for x86_64-linux-gnu-thread-multi
          (with 69 registered patches)



          Same thing in C - https://github.com/james28909/count/blob/master/count.c



          EDIT:



          Actually here is another, BETTER, example given by BrowserUK at perlmonks - https://www.perlmonks.org/?node_id=1159266 - It seems to run faster than both examples/answers given.



          use strict;
          use Time::HiRes qw[ time ];

          my $start = time;

          open I, '<:raw', $ARGV[ 0 ];

          my @seen;

          while( read( I, my $buf, 16384 ) ) {
          ++$seen[$_] for unpack 'C*', $buf;
          }
          printf "Took %f secsn", time() - $start;





          share|improve this answer























          • Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
            – ikegami
            Nov 20 at 1:21






          • 1




            $n++ is also very wasteful. Much better just to sum the hash values after the loop.
            – ikegami
            Nov 20 at 1:21












          • It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
            – james28909
            Nov 21 at 0:50










          • original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
            – james28909
            Nov 21 at 3:06










          • The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
            – ikegami
            Nov 21 at 3:39





















          2














          use List::Util qw( sum );

          use constant BLOCK_SIZE => 4*1024*1024;

          open(my $fh, '<:raw', $qfn)
          or die("Can't open "$qfn": $!n");

          my @counts = (0) x 256;
          while (1) {
          my $rv = sysread($fh, my $buf, BLOCK_SIZE);
          die($!) if !defined($rv);
          last if !$rv;

          ++$counts[$_] for unpack 'C*', $buf;
          }

          my $N = sum @counts;





          share|improve this answer























          • I am kinda confused, I can only seem to get the total bytes?
            – BwE
            Nov 4 at 5:43










          • While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
            – ikegami
            Nov 4 at 5:58












          • Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
            – BwE
            Nov 4 at 6:01













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53137917%2fhow-would-one-calculate-binary-statistics-in-perl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Heres another way to do it:



          use strict;
          use warnings;
          use Time::HiRes qw( time );

          $/ = 1;

          open my $file, '<', shift;
          binmode $file;

          my %seen;

          my $start = time();
          my $n;

          while (<$file>) {
          $seen{$_} ++;
          $n++;
          }
          my $end = time();

          for ( sort keys %seen ) {
          printf( "%s%s%.2f%sn", uc( unpack( 'H*', $_ ) ), " seen $seen{$_} times - ", $seen{$_} / $n * 100, "%" );
          }

          printf( "took %.3f seconds!n", $end - $start );


          output:



          ...
          ...
          F8 seen 46475 times - 0.28%
          F9 seen 46611 times - 0.28%
          FA seen 46703 times - 0.28%
          FB seen 48902 times - 0.29%
          FC seen 46829 times - 0.28%
          FD seen 47707 times - 0.28%
          FE seen 47276 times - 0.28%
          FF seen 1752333 times - 10.44%
          took 2.374 seconds!


          This is (WSL in windows) perl 5.22.1 built for x86_64-linux-gnu-thread-multi
          (with 69 registered patches)



          Same thing in C - https://github.com/james28909/count/blob/master/count.c



          EDIT:



          Actually here is another, BETTER, example given by BrowserUK at perlmonks - https://www.perlmonks.org/?node_id=1159266 - It seems to run faster than both examples/answers given.



          use strict;
          use Time::HiRes qw[ time ];

          my $start = time;

          open I, '<:raw', $ARGV[ 0 ];

          my @seen;

          while( read( I, my $buf, 16384 ) ) {
          ++$seen[$_] for unpack 'C*', $buf;
          }
          printf "Took %f secsn", time() - $start;





          share|improve this answer























          • Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
            – ikegami
            Nov 20 at 1:21






          • 1




            $n++ is also very wasteful. Much better just to sum the hash values after the loop.
            – ikegami
            Nov 20 at 1:21












          • It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
            – james28909
            Nov 21 at 0:50










          • original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
            – james28909
            Nov 21 at 3:06










          • The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
            – ikegami
            Nov 21 at 3:39


















          0














          Heres another way to do it:



          use strict;
          use warnings;
          use Time::HiRes qw( time );

          $/ = 1;

          open my $file, '<', shift;
          binmode $file;

          my %seen;

          my $start = time();
          my $n;

          while (<$file>) {
          $seen{$_} ++;
          $n++;
          }
          my $end = time();

          for ( sort keys %seen ) {
          printf( "%s%s%.2f%sn", uc( unpack( 'H*', $_ ) ), " seen $seen{$_} times - ", $seen{$_} / $n * 100, "%" );
          }

          printf( "took %.3f seconds!n", $end - $start );


          output:



          ...
          ...
          F8 seen 46475 times - 0.28%
          F9 seen 46611 times - 0.28%
          FA seen 46703 times - 0.28%
          FB seen 48902 times - 0.29%
          FC seen 46829 times - 0.28%
          FD seen 47707 times - 0.28%
          FE seen 47276 times - 0.28%
          FF seen 1752333 times - 10.44%
          took 2.374 seconds!


          This is (WSL in windows) perl 5.22.1 built for x86_64-linux-gnu-thread-multi
          (with 69 registered patches)



          Same thing in C - https://github.com/james28909/count/blob/master/count.c



          EDIT:



          Actually here is another, BETTER, example given by BrowserUK at perlmonks - https://www.perlmonks.org/?node_id=1159266 - It seems to run faster than both examples/answers given.



          use strict;
          use Time::HiRes qw[ time ];

          my $start = time;

          open I, '<:raw', $ARGV[ 0 ];

          my @seen;

          while( read( I, my $buf, 16384 ) ) {
          ++$seen[$_] for unpack 'C*', $buf;
          }
          printf "Took %f secsn", time() - $start;





          share|improve this answer























          • Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
            – ikegami
            Nov 20 at 1:21






          • 1




            $n++ is also very wasteful. Much better just to sum the hash values after the loop.
            – ikegami
            Nov 20 at 1:21












          • It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
            – james28909
            Nov 21 at 0:50










          • original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
            – james28909
            Nov 21 at 3:06










          • The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
            – ikegami
            Nov 21 at 3:39
















          0












          0








          0






          Heres another way to do it:



          use strict;
          use warnings;
          use Time::HiRes qw( time );

          $/ = 1;

          open my $file, '<', shift;
          binmode $file;

          my %seen;

          my $start = time();
          my $n;

          while (<$file>) {
          $seen{$_} ++;
          $n++;
          }
          my $end = time();

          for ( sort keys %seen ) {
          printf( "%s%s%.2f%sn", uc( unpack( 'H*', $_ ) ), " seen $seen{$_} times - ", $seen{$_} / $n * 100, "%" );
          }

          printf( "took %.3f seconds!n", $end - $start );


          output:



          ...
          ...
          F8 seen 46475 times - 0.28%
          F9 seen 46611 times - 0.28%
          FA seen 46703 times - 0.28%
          FB seen 48902 times - 0.29%
          FC seen 46829 times - 0.28%
          FD seen 47707 times - 0.28%
          FE seen 47276 times - 0.28%
          FF seen 1752333 times - 10.44%
          took 2.374 seconds!


          This is (WSL in windows) perl 5.22.1 built for x86_64-linux-gnu-thread-multi
          (with 69 registered patches)



          Same thing in C - https://github.com/james28909/count/blob/master/count.c



          EDIT:



          Actually here is another, BETTER, example given by BrowserUK at perlmonks - https://www.perlmonks.org/?node_id=1159266 - It seems to run faster than both examples/answers given.



          use strict;
          use Time::HiRes qw[ time ];

          my $start = time;

          open I, '<:raw', $ARGV[ 0 ];

          my @seen;

          while( read( I, my $buf, 16384 ) ) {
          ++$seen[$_] for unpack 'C*', $buf;
          }
          printf "Took %f secsn", time() - $start;





          share|improve this answer














          Heres another way to do it:



          use strict;
          use warnings;
          use Time::HiRes qw( time );

          $/ = 1;

          open my $file, '<', shift;
          binmode $file;

          my %seen;

          my $start = time();
          my $n;

          while (<$file>) {
          $seen{$_} ++;
          $n++;
          }
          my $end = time();

          for ( sort keys %seen ) {
          printf( "%s%s%.2f%sn", uc( unpack( 'H*', $_ ) ), " seen $seen{$_} times - ", $seen{$_} / $n * 100, "%" );
          }

          printf( "took %.3f seconds!n", $end - $start );


          output:



          ...
          ...
          F8 seen 46475 times - 0.28%
          F9 seen 46611 times - 0.28%
          FA seen 46703 times - 0.28%
          FB seen 48902 times - 0.29%
          FC seen 46829 times - 0.28%
          FD seen 47707 times - 0.28%
          FE seen 47276 times - 0.28%
          FF seen 1752333 times - 10.44%
          took 2.374 seconds!


          This is (WSL in windows) perl 5.22.1 built for x86_64-linux-gnu-thread-multi
          (with 69 registered patches)



          Same thing in C - https://github.com/james28909/count/blob/master/count.c



          EDIT:



          Actually here is another, BETTER, example given by BrowserUK at perlmonks - https://www.perlmonks.org/?node_id=1159266 - It seems to run faster than both examples/answers given.



          use strict;
          use Time::HiRes qw[ time ];

          my $start = time;

          open I, '<:raw', $ARGV[ 0 ];

          my @seen;

          while( read( I, my $buf, 16384 ) ) {
          ++$seen[$_] for unpack 'C*', $buf;
          }
          printf "Took %f secsn", time() - $start;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 3:05

























          answered Nov 9 at 8:39









          james28909

          1591315




          1591315












          • Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
            – ikegami
            Nov 20 at 1:21






          • 1




            $n++ is also very wasteful. Much better just to sum the hash values after the loop.
            – ikegami
            Nov 20 at 1:21












          • It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
            – james28909
            Nov 21 at 0:50










          • original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
            – james28909
            Nov 21 at 3:06










          • The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
            – ikegami
            Nov 21 at 3:39




















          • Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
            – ikegami
            Nov 20 at 1:21






          • 1




            $n++ is also very wasteful. Much better just to sum the hash values after the loop.
            – ikegami
            Nov 20 at 1:21












          • It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
            – james28909
            Nov 21 at 0:50










          • original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
            – james28909
            Nov 21 at 3:06










          • The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
            – ikegami
            Nov 21 at 3:39


















          Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
          – ikegami
          Nov 20 at 1:21




          Ow, reading one byte at a time? At least the system reads 8 KiB at a time. My solution has it read much larger blocks for better speed.
          – ikegami
          Nov 20 at 1:21




          1




          1




          $n++ is also very wasteful. Much better just to sum the hash values after the loop.
          – ikegami
          Nov 20 at 1:21






          $n++ is also very wasteful. Much better just to sum the hash values after the loop.
          – ikegami
          Nov 20 at 1:21














          It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
          – james28909
          Nov 21 at 0:50




          It honestly wasnt meant to be a drop in solution. its just another way to do it. the $n was just a way to help calculate percentages of each byte seen. you are right... filesize could easily be obtained other ways. i know what to do to change it, but in all honesty it was not meant to be a direct code replacement, just an example of another way to count bytes. thanks
          – james28909
          Nov 21 at 0:50












          original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
          – james28909
          Nov 21 at 3:06




          original post edited. added an example from perlmonks, that i asked along time ago. just cross referencing.
          – james28909
          Nov 21 at 3:06












          The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
          – ikegami
          Nov 21 at 3:39






          The bottom snippet is better, but still has two issues. 1) read 16384 is silly, since read reads in 4 KiB or 8KiB chunks depending on your version of Perl. Best to use sysread to perform a single call to the OS. sysread also has less overhead. 2) If a value doesn't occur in the file you get an undefined count instead of zero. So it's best to initialize the counts to zero first. /// After you make these two fixes, you get my answer.
          – ikegami
          Nov 21 at 3:39















          2














          use List::Util qw( sum );

          use constant BLOCK_SIZE => 4*1024*1024;

          open(my $fh, '<:raw', $qfn)
          or die("Can't open "$qfn": $!n");

          my @counts = (0) x 256;
          while (1) {
          my $rv = sysread($fh, my $buf, BLOCK_SIZE);
          die($!) if !defined($rv);
          last if !$rv;

          ++$counts[$_] for unpack 'C*', $buf;
          }

          my $N = sum @counts;





          share|improve this answer























          • I am kinda confused, I can only seem to get the total bytes?
            – BwE
            Nov 4 at 5:43










          • While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
            – ikegami
            Nov 4 at 5:58












          • Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
            – BwE
            Nov 4 at 6:01


















          2














          use List::Util qw( sum );

          use constant BLOCK_SIZE => 4*1024*1024;

          open(my $fh, '<:raw', $qfn)
          or die("Can't open "$qfn": $!n");

          my @counts = (0) x 256;
          while (1) {
          my $rv = sysread($fh, my $buf, BLOCK_SIZE);
          die($!) if !defined($rv);
          last if !$rv;

          ++$counts[$_] for unpack 'C*', $buf;
          }

          my $N = sum @counts;





          share|improve this answer























          • I am kinda confused, I can only seem to get the total bytes?
            – BwE
            Nov 4 at 5:43










          • While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
            – ikegami
            Nov 4 at 5:58












          • Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
            – BwE
            Nov 4 at 6:01
















          2












          2








          2






          use List::Util qw( sum );

          use constant BLOCK_SIZE => 4*1024*1024;

          open(my $fh, '<:raw', $qfn)
          or die("Can't open "$qfn": $!n");

          my @counts = (0) x 256;
          while (1) {
          my $rv = sysread($fh, my $buf, BLOCK_SIZE);
          die($!) if !defined($rv);
          last if !$rv;

          ++$counts[$_] for unpack 'C*', $buf;
          }

          my $N = sum @counts;





          share|improve this answer














          use List::Util qw( sum );

          use constant BLOCK_SIZE => 4*1024*1024;

          open(my $fh, '<:raw', $qfn)
          or die("Can't open "$qfn": $!n");

          my @counts = (0) x 256;
          while (1) {
          my $rv = sysread($fh, my $buf, BLOCK_SIZE);
          die($!) if !defined($rv);
          last if !$rv;

          ++$counts[$_] for unpack 'C*', $buf;
          }

          my $N = sum @counts;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 4 at 5:23

























          answered Nov 4 at 5:17









          ikegami

          261k11176396




          261k11176396












          • I am kinda confused, I can only seem to get the total bytes?
            – BwE
            Nov 4 at 5:43










          • While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
            – ikegami
            Nov 4 at 5:58












          • Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
            – BwE
            Nov 4 at 6:01




















          • I am kinda confused, I can only seem to get the total bytes?
            – BwE
            Nov 4 at 5:43










          • While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
            – ikegami
            Nov 4 at 5:58












          • Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
            – BwE
            Nov 4 at 6:01


















          I am kinda confused, I can only seem to get the total bytes?
          – BwE
          Nov 4 at 5:43




          I am kinda confused, I can only seem to get the total bytes?
          – BwE
          Nov 4 at 5:43












          While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
          – ikegami
          Nov 4 at 5:58






          While that's what $N contains, @counts contains the information you requested. $counts[0x00] contains the count of 00 bytes, $counts[0x01] contains the count of 01 bytes, ..., and $counts[0xFF] contains the count of FF bytes.
          – ikegami
          Nov 4 at 5:58














          Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
          – BwE
          Nov 4 at 6:01






          Ahhhhh sorry! Yes! I rushed ahead a bit. Works like a charm! Takes about 7 seconds to calculate. You're a life saver!
          – BwE
          Nov 4 at 6:01




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53137917%2fhow-would-one-calculate-binary-statistics-in-perl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'