Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created May 9, 2013 11:30
Show Gist options
  • Save ynonp/5546947 to your computer and use it in GitHub Desktop.
Save ynonp/5546947 to your computer and use it in GitHub Desktop.
perl intel 8/5
use strict;
use warnings;
use v5.14;
my $RE_PATTERN = qr {
#
# This is a cool regular pattern
#
# DIGIT DIIGT DIGIT Ends with an x
[0-9] [0-9] [0-9] x$
}x;
my $IS_A_NUMBER = qr {
( \b \d+ \b )
}x;
my $HAS_DOUBLE_LETTER = qr {
([a-z])\1
}x;
my $HAS_DOUBLE_DIGIT = qr {
00 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99
}x;
while (<>) {
my @numbers = /$IS_A_NUMBER/g;
say "Got: @numbers";
}
use strict;
use warnings;
use v5.14;
use List::Util qw/sum/;
sub print_times {
my ( $text, $times ) = @_;
$times ||= 5;
say $text x $times;
}
sub is_digit {
my ( $char ) = @_;
return if length( $char ) > 1;
return ( $char ge '0' && $char le '9' );
}
sub count_digits {
my ( $string ) = @_;
my $digits = 0;
while ( $string ) {
my $next_char = chop( $string );
$digits++ if is_digit( $next_char );
}
return $digits;
}
sub sum_even {
my ( @numbers ) = @_;
my @only_even = grep { $_ % 2 == 0 } @numbers;
return sum ( @only_even );
}
sub count_chars {
my ( @strings ) = @_;
my @lengths = map length, @strings;
# Using custom map
my @sqr_length = map { length($_) * length($_) } @strings;
return sum( @lengths );
}
say count_digits('foo 7 22 bar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment