I have numbers like these:
15-84-315-164
11-76-132-310
44-20-958-2732
And I need to know how many odd numbers are in a row.
For example in 15-84-315-164 there are 6 odd numbers.
I want to use perl and maybe regex for the solution.
use strict;
use warnings;
use feature 'say';
my @data = <DATA>;
chomp @data;
process_line($_) for @data;
sub process_line {
my $data = shift;
my $count;
my @digits = $data =~ /(\d)/g;
for my $digit (@digits) {
$digit%2 ? $count->{odd}++ : $count->{even}++;
}
say ' Odd: ' . $count->{odd} . ' Even: ' . $count->{even} . ' Line: ' . $data;
}
__DATA__
15-84-315-164
11-76-132-310
44-20-958-2732
Output
Odd: 6 Even: 4 Line: 15-84-315-164
Odd: 7 Even: 3 Line: 11-76-132-310
Odd: 4 Even: 7 Line: 44-20-958-2732
my $string = "15-84-315-164";
my ($number) = scalar( @{[ $string=~/(1|3|5|7|9)/gi ]} );
print $number
Below is the regex.
(1|3|5|7|9)
What this basically does is finds if odd number is present.
Once we have the regex, we then just count the number of times the regex appeared by using scalar.