Does this do what you are requesting?

Processing script

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use File::Basename; use File::Spec; use Getopt::Long; $| = 1; # Next 3 lines are my preference for debugging srand(); $Data::Dumper::Deepcopy = 1; $Data::Dumper::Sortkeys = 1; my $input_filename; my $output_filename; GetOptions( q{input_file|input_filename|input:s} => \$input_filename, q{output_file|output_filename|output:s} => \$output_filename, q{help} => \&help_output, ); sub help_output { print qq{Usage:\n\t$0 } . qq{--input_file=input.file.name } . qq{--output_file=output.file.name\n\n} . qq{\t\t--input_file - file name to read data from\n\n} . qq{\t\t--output_file - file name to write data to\n\n}; exit; } if ( not( defined $input_filename and defined $output_filename ) ) { &help_output; exit -1; } open my $inf, q{<}, $input_filename or die $!; open my $outf, q{>}, $output_filename or die $!; my $format_string = qq{Character: %s Count: %d\n}; my $line_value; my $line_value_count = 0; while ( my $line = <$inf> ) { chomp $line; if ( not defined $line_value ) { $line_value = $line; $line_value_count = 0; } if ( $line_value ne $line ) { print $outf sprintf $format_string, $line_value, $line_value_count; $line_value = $line; $line_value_count = 0; } $line_value_count++; } print $outf sprintf $format_string, $line_value, $line_value_count; close $outf; close $inf;

Test file generation snippets:

# file consisting of 50 0s perl -le 'foreach my $i ( 1 .. 50 ) { print q{0}; }' > file_0.txt
# file consisting of 50 1s perl -le 'foreach my $i ( 1 .. 50 ) { print q{1}; }' > file_1.txt
# file containing mix of 0s and 1s perl -le 'srand(); my $f = 0; foreach my $i ( 1 .. 50 ) { if ( $i % 5 +== 0 ) { $f++; $f %= 2; } print $f; }' > file_mix.txt

Sample output (file_0.txt)

Character: 0 Count: 50

Sample output (file_1.txt)

Character: 1 Count: 50

Sample output (file_mix.txt)

Character: 0 Count: 4 Character: 1 Count: 5 Character: 0 Count: 5 Character: 1 Count: 5 Character: 0 Count: 5 Character: 1 Count: 5 Character: 0 Count: 5 Character: 1 Count: 5 Character: 0 Count: 5 Character: 1 Count: 5 Character: 0 Count: 1

Hope that helps.


In reply to Re: Counting iterations of 1 and 0 by atcroft
in thread Counting iterations of 1 and 0 by AwsedreswA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.