Message Boards Message Boards

0
|
4482 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Can we Substitue numbers for letters and back?

Posted 9 years ago

Lets say I have a small list of numbers converted to digits

p={{1,3,9,8,5,4,2,7,6},{1,5,2,8,4,3,7,6,9},{1,5,7,3,2,6,8,4,9}}

and I also have 2 Words say txt2={INTRODUCE,REDUCTION}

I want to map the first set of digits to the first word so

Thread[Characters[txt2[[1]]] -> p[[1]]]

gives me

 {I->1,N->3,T->9,R->8,O->5,D->4,U->2,C->7,E->6}

I now wish to map those back to see what REDUCTION in digits would be i.e.

REDUCTION = 864279153

and repeat for the other digits sets in list p

The Thread function seems to go part way but can't see how to reverse the process any help would be much appreciated.

Paul.

POSTED BY: Paul Cleary
2 Replies

ReplaceAll or /. ?

p = {{1, 3, 9, 8, 5, 4, 2, 7, 6}, {1, 5, 2, 8, 4, 3, 7, 6, 9}, {1, 5, 7, 3, 2, 6, 8, 4, 9}};
txt = {"INTRODUCE", "REDUCTION"};
FromDigits[Characters[txt[[2]]] /. Thread[Characters[txt[[1]]] -> p[[1]]]]
(*864279153*)

Another way is StringReplace but that outputs number as string:

StringReplace["REDUCTION", Thread[Characters[txt[[1]]] -> ToString /@ p[[1]]]]
(*"864279153"*)
POSTED BY: Vitaliy Kaurov
Posted 9 years ago

Excellent, thank you Vitaliy, already amended to account for the list.

POSTED BY: Paul Cleary
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract