# On Perl's Home Ground **Challenge 230 solutions in Perl by Matthias Muth** ## Task 1: Separate Digits > You are given an array of positive integers.
> Write a script to separate the given array into single digits.
>
> Example 1
> Input: @ints = (1, 34, 5, 6)
> Output: (1, 3, 4, 5, 6)
>
> Example 2
> Input: @ints = (1, 24, 51, 60)
> Output: (1, 2, 4, 5, 1, 6, 0)
This really is a simple challenge for Perl.
We can make full use of perl's type flexibility to convert the numbers in the list into strings for concatenation, without even needing to ask for it, and then split the result into a list of characters, just like that: ```perl use v5.36; sub separate_digits( @ints ) { return split "", join "", @ints; } ``` ## Task 2: Count Words > You are given an array of words made up of alphabetic characters and a prefix.
> Write a script to return the count of words that starts with the given prefix.
>
> Example 1
> Input: @words = ("pay", "attention", "practice", "attend")
> $prefix = "at"
> Ouput: 2
> Two words "attention" and "attend" starts with the given prefix "at".
>
> Example 2
> Input: @words = ("janet", "julia", "java", "javascript")
> $prefix = "ja"
> Ouput: 3
> Three words "janet", "java" and "javascripr" starts with the given prefix "ja".
Again, we are on Perl's home ground.
We can use a very simple regular expression to decide whether a word starts with the `$prefix`, and get (and return) the number of matches from `grep` by putting it into a scalar context. ```perl use v5.36; sub count_words( $words, $prefix ) { return scalar grep /^$prefix/, @$words; } ``` ## Note 1: Perl version I am using ```perl use v5.36; ``` to get function prototypes, `say` and many other useful 'modern' things, without needing to list them all. Love it!
If you don't have Perl 5.36, but at least 5.20, you can use this: ```perl use strict; use warnings; use feature 'say'; use feature 'signatures'; no warnings 'experimental::signatures'; ``` ## Note 2: TestExtractor.pm My [code](perl) also includes [`TestExtractor.pm`](perl/TestExtractor.pm), which extracts the example test data from a text version of the challenge tasks (extracted from the [website](https://theweeklychallenge.org/) by another script every monday ;-), and runs the tests. ## #### **Thank you for the challenge!**