TASK #1: Acronyms
You are given an array of words and a word.
Write a script to return true if concatenating the first letter of each word in the given array matches the given word, return false otherwise.
#!/usr/bin/perl use strict; use warnings; sub acronyms { # $words is a reference to an array of words, e.g.: ['Perl','Weekly','Challenge'] # $acronym is a string we want to compare against, here 'PWC' my ($words, $acronym) = @_;# @$words -> turn the array reference into a list of words: ('Perl', 'Weekly', 'Challenge') # map {...} -> take the first letter of each word: ('P', 'W', 'C') # join '' -> glue the letters together: 'PWC' # eq $acronym -> compare with the expected acronym # result -> true if they match, false if they don't join('', map { substr $_, 0, 1 } @$words) eq $acronym; } # Tests print acronyms(['Perl','Weekly','Challenge'],'PWC') ? "true\n" : "false\n"; print acronyms(['Bob','Charlie','Joe'],'BCJ') ? "true\n" : "false\n"; print acronyms(['Morning','Good'],'MM') ? "true\n" : "false\n";