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

Re: A little golfing challenge: Replacing letters with numbers (edit)

by Veltro (Hermit)
on Feb 21, 2019 at 19:37 UTC ( #1230323=note: print w/replies, xml ) Need Help??


in reply to A little golfing challenge: Replacing letters with numbers

use strict ; use warnings ; my $input = "NDDDDTSVCLGTRQCSWFAGCTNRTWNSSA 0" ; my @F = split'',$input ; my @ar ; @ar[map { ord } split '','ARNDBCEQZGHILKMFPSTWYV'] = 1..127 ; foreach ( @F ) { print( ($ar[ord $_] // $_ ) . " " ) ; }

But I can't figure out how to put this in a one-liner, does anyone want to try?

edit Feb 22, 2019 at 10:20 UTC:

Up till now I have:

perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '($_=$F[0])=~s/(.)/($ar[ord $1])." "/ge' input.txt # or perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '($_=$F[0])=~s/(.)/(defined $ar[ord $1] ? $ar[ord $1] : $1 ) +." "/ge' input.txt

As rsFalse indicated in his reply below there is a trailing space. The other thing is that everything after a space is gone missing and I can't figure out why.

edit Feb 22, 2019 at 10:49 UTC:

Thanks Eily and Discipulus for pointing out -MO=Deparse to me. The mistake that I made was: $_=$F[0] So this should work now:

# 'defined' is optional: perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '$_=~s/(.)/(defined $ar[ord $1] ? $ar[ord $1]." " : $1 )/ge' + input.txt

One more edit: Also note that I specifically crafted this in this manner so that this would also work:

perl '-M5;@ar[map { ord } split "","\0V\0B\0L\0\0\0Q\0\0\0\0\0\0\0E\0\ +0\0\0\0\0\0\0\0\0P\0\0I\0\0\0\0\0\0Z\0H\0\0\0\0\0\0\0\0Y\0J\0\0S\0\0\ +0\0\0D\0\0\0X\0\0\0\0\0K\0\0\0R\0\0\0\0W\0\0\0\0\0U\0\0\0\0A\0\0M\0\0 +\0F\0\0\0C\0\0T\0\0\0\0O\0\0\0\0\0\0\0\0\0\0N\0G\0\0\0\0"] = 0..127' +-alpe '$_=~s/(.)/($ar[ord $1] ? $ar[ord $1]." " : $1 )/ge' input.txt

Replies are listed 'Best First'.
Re^2: A little golfing challenge: Replacing letters with numbers
by rsFalse (Hermit) on Feb 21, 2019 at 21:13 UTC
    Hello, Veltro,

    Nice idea with 'ord'!
    I've tried your code, and it outputs:
    3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 +18 1 0
    Here the newline is absent, and one extra trailing space, these are not problems I think. But some extra characters at the ending does solution wrong. (upd.:) Here I would suggest to modify printing line to one which converts undefined array elements to zero-length string, e.g.:
    print( $ar[ord $_] =~ s/.\K$/ /r );
    Which outputs (with extra space and without newline):
    3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 +18 1
    upd. ...or back to '//' op (to avoid uninitialized value warnings):
    print( ( $ar[ord $_] // '' ) =~ s/.\K$/ /r );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-11-30 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?