Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

y//./ gives two points instead of one

by rsFalse (Chaplain)
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 (Patriarch) 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 (Sage) 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!


    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.


    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


    ) 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?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-21 09:11 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.