Perl iso-8859-1 string comparison

I wrote a small program to go through / usr / share / dict / words finding palindromes

while(<>){
  chomp;
  print "$_\n" if $_ eq reverse;
}

However, this does not work for a list of Danish words encoded in Latin-1 (ISO-8859-1). Just wondering, how am I going to make it work?

+3
source share
1 answer

Use locale ? And perhaps also include the Unicode flag in STDIN:

use Modern::Perl;
use locale;
binmode(STDIN, ":utf8");
while (<>) {
    chomp;
    say if $_ eq reverse;
}

Without binmodeit, it could be a nice one-liner:

perl -Mlocale -nE 'chomp; say if $_ eq reverse'
+3
source

Source: https://habr.com/ru/post/1731453/


All Articles