#!/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;
####
# 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
####
Character: 0 Count: 50
####
Character: 1 Count: 50
####
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