Character replacement

Hi, all !
This example given in the documentation’s page 20.8 does not work, at least in the version I’m using, which is 6L38 :

replace character number (number) in (text) with (text)

This phrase acts on the named text by placing the given text in place of the Nth character, counting from 1. Example:

let V be “mope”;
replace character number 3 in V with “lecul”;
say V;

says “molecule”.

The error message says that this works only on snippets.

So, how can I replace the character number N of a text variable with another ?
Thanks in advance.

To clarify: When you run that code, does it print “molecule” ​or not?

You say that it says “molecule” but also that you’re getting an error message.

Is it a run-time error or a failure to compile?

When I run your code (in 6M62), it prints “molecule” and there is no error message.

-Wade

Thanks, Wade.
Actually, I didn’t use the example’s code “as is”. This is the code I used:

To UpShift:
let St be “gnou”;
let N be 2;
replace the character number N in St with “l”;
say “[St]”;

In my opinion, this should change the string “gnou” to “glou” but it doesn’t even compile and I get the following error message:

Problem. In ‘replace the character number N in St with “l”’, the phrase ‘character number N in St’ doesn’t seem to fit: I was hoping it would be a snippet, but in fact it’s a text.

I was trying to match this phrase:

character number (n - number) in (st - text)

I recognised:

n = a temporary named value , holding a number

st = a temporary named value , holding a text

Because of this problem, the source could not be translated into a working game. (Correct the source text to remove the difficulty and click on Go once again.)

Can you help ?

Omit the the. The phrase is replace character number <number> in <text> with <text>, and you can’t insert an article between adjacent words of the phrase: I7 is just as unforgiving of replace the character number as it would be of replace character the number.

You can say replace character number the N in the St with the “l”; because all of those the's are before parameters.

5 Likes

That works ! Many thanks, Zed.