Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare 2 strings by each characters in perl

basically I want to compare

$a = "ABCDE";
$b = "--(-)-";

and get output CE.

i.e where ever parentheses occur the characters of $a should be taken.

like image 900
Kiran K Telukunta Avatar asked Dec 10 '22 03:12

Kiran K Telukunta


1 Answers

One of the rare uses of the bitwise or-operator.

# magic happens here  ↓
perl -E'say (("ABCDE" | "--(-)-" =~ tr/-()/\377\000/r) =~ tr/\377//dr)'

prints CE.

Use this for golfing purposes only, AHA’s solution is much more maintainable.

like image 157
daxim Avatar answered Dec 30 '22 21:12

daxim