Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

y//./ gives two points instead of one

by rsFalse (Friar)
on Nov 16, 2015 at 21:07 UTC ( #1147843=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

I was translating morse code, and each centered-point was translated to two (not one) ascii dots. Can anybody explain?
use warnings; use strict; $_ = '|'; print y//./r, "\n"; print s//./gr, "\n";
OUTPUT: ..|.. .|.

Replies are listed 'Best First'.
Re: y//./ gives two points instead of one
by ikegami (Pope) on Nov 16, 2015 at 21:38 UTC

    You think you have

    $_ = '|'; print y//./r, "\n"; print s//./gr, "\n";

    but you actually have

    $_ = '·|·'; print y/·/./r, "\n"; print s/·/./gr, "\n";

    This is because you treated the file as UTF-8 when you viewed it, but you didn't tell Perl to do the same by adding use utf8;.

      Thanks for explanation!
Re: y//./ gives two points instead of one
by LanX (Archbishop) on Nov 16, 2015 at 21:21 UTC
    > Can anybody explain?

    use utf8 ! :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    update

    Without utf8 flag you are forcing perl to transliterate a 2 byte character to a 1 byte character.

    My interpretation is that this 1 byte char is doubled to keep the counters in sync.

    update

    same effect here

    use warnings; use strict; #use utf8; $_ = '|'; print y//$/r, "\n";

    Careful with such experiments, I was able to make my Perl (5.14) coredump

    update

    ) ikegami's explanation is better, you are transliterating the first and second byte for separately to .

    DB<123> unpack 'H*','' => "c2b7"

    Got confused about the tr flags and their defaults.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1147843]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2019-07-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?