Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl, string replacement

Tags:

string

perl

I want to convert each letter in a sentence to a certain letter depending on if it is a consonant or a vowel, where vowels are AEIOU.

So if I have a string

$string = 'Hello'

I would like to see

$string = 'CVCCV'

As a result.

I know I can use:

$string =~ s/A/V/
$string =~ s/B/C/
$string =~ s/C/C/

and so on to check and convert each letter individually, but surely there must be a more efficient way to do this.

like image 555
Brian Avatar asked Nov 28 '22 03:11

Brian


1 Answers

normalize case, then apply the transliteration operator:

$string = lc $string;
$string =~ tr/aeioua-z/VVVVVC/;
like image 165
tadmc Avatar answered Dec 25 '22 03:12

tadmc