blob: 8850b2c7241b46c1005f92eaf96b7d464f6a2749 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#! /opt/local/bin/perl
#
# stones_and_jewels.pl
#
# task:Create a script that accepts two strings, let us call it,
# “stones” and “jewels”. It should print the count of “alphabet”
# from the string “stones” found in the string “jewels”. For
# example, if your stones is “chancellor” and “jewels” is
# “chocolate”, then the script should print “8”. To keep it
# simple, only A-Z,a-z characters are acceptable. Also make the
# comparison case sensitive.
#
# 2019 colin crain
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
use warnings;
use strict;
use feature ":5.26";
## ## ## ## ## MAIN
my ($stone, $jewel) = @ARGV;
my $jewels = { map { $_, 1 } (split //, $jewel) };
my @gravel = split //, $stone;
my $result = scalar (grep { $jewels->{$_} } @gravel);
say $result;
## this could alternately be done in just two lines:
# my $jewels = { map { $_, 1 } (split //, $ARGV[1]) };
# say scalar (grep { $jewels->{$_} } (split //, $ARGV[0]));
|