Length is: '10427'. Missing count: '0' Duplicate count: '424' #### # Will return an analysis of the string. # Usage: print analyse( $string ); sub analyse { # Make an array from the number and create hash. my $number = shift; my @string = split //, $number; my %count = (); # Work over the number adding values to the hash. while (1) { my $candidate = $string[0].$string[1].$string[2].$string[3]; $count{$candidate}++; # Get rid of first element of the array. shift @string; # Break out at the end of array. last if scalar @string < 4; } # Tally up the number of duplicates and missing numbers. my $duplicates = 0; my $missing = 0; for ( 0 .. 9999 ) { my $k = 0 x ( 4 - length $_ ) . $_; my $v = $count{$k}; unless ( defined $v ) { $missing++; next; } $duplicates += ( $v - 1 ) if $v > 1; } # Create the summary. my $text; $text .= " Length is: " . length($number) . "\n"; $text .= " Missing count: " . $missing . "\n"; $text .= "Duplicate count: " . $duplicates . "\n\n"; return $text; }