Right now, my code looks like this:


Code:
:"<theta><space>→Str1
:sub(Str2,1,3<length of map>)+sub(Str1,1,1)+sub(Str2,3H+2,length(Str2)-length(sub(Str2,1,3H


That looks nasty. Is there an easier way?
What exactly are you trying to get this to replace?
The theta in the first string should replace the spot in the second string.
Is there a chance you could post what you have in Str2 so I can test out something?
"1234567890123456879012345678901234567890

10->H
As far as I know, sub() + [thing to add] + sub() is the best way to do it. I seem to even recall having to have cases for replacing the first or last character(s) in a string, but with a space-pad on front and back you can avoid that.
KermMartian wrote:
As far as I know, sub() + [thing to add] + sub() is the best way to do it. I seem to even recall having to have cases for replacing the first or last character(s) in a string, but with a space-pad on front and back you can avoid that.


I have been managing to replace what he wants, but after that, everything after the replaced character is gone, trying to get that fixed and I can post what I came up with.
Sonlen wrote:
KermMartian wrote:
As far as I know, sub() + [thing to add] + sub() is the best way to do it. I seem to even recall having to have cases for replacing the first or last character(s) in a string, but with a space-pad on front and back you can avoid that.


I have been managing to replace what he wants, but after that, everything after the replaced character is gone, trying to get that fixed and I can post what I came up with.
That's why you need the second sub()...?
KermMartian wrote:
Sonlen wrote:
KermMartian wrote:
As far as I know, sub() + [thing to add] + sub() is the best way to do it. I seem to even recall having to have cases for replacing the first or last character(s) in a string, but with a space-pad on front and back you can avoid that.


I have been managing to replace what he wants, but after that, everything after the replaced character is gone, trying to get that fixed and I can post what I came up with.
That's why you need the second sub()...?


Yeah, and I have quite a bit, I am going to keep tweaking it though.

This works perfectly for me:

Code:

:"<theta> →Str1    "1 space"
:"1234567890123456789012345678901234567890→Str2
:length(Str2→G
:length(<map string here>→H  "in this case 10"
:sub(Str2,1,3H)+sub(Str1,1,1)+sub(Str2,3H+2,length(sub(Str2,3H,G-(3H+2


That outputs:


Code:
123456789012345678901234567890<theta>23456789
Indeed, that's more or less what I expected regarding the three subs necessary. However, I'm not convinced of the correctness of that example for arbitrary G.

Code:

Prompt Str1,Str2,A
"
If A>=1
Ans+sub(Str2,1,A
Ans+Str1
If length(Str2)-A-1
Ans+sub(Str2,A+1,length(Str2)-A-1
sub(Ans,2,length(Ans)-1→Str2
Disp Str2

That code ought to be fool-proof. You can of course optimize it, but at the risk of errors. I just didn't feel like leaving this code with the potential of errors.
Very nice one, Mufinator! Thanks for that; if you're sure it works for all use cases, you should put it in the Code Fragments topic.
To replace the token at position X in Str1 with Str2:


Code:
:"░"+Str1+"░
:sub(Ans,1,X)+Str2+sub(Ans,X+2,length(Ans)-X-1
:sub(Ans,2,length(Ans)-2


[EDIT] – Whoops, forgot the start argument on one of them.
KermMartian wrote:
Indeed, that's more or less what I expected regarding the three subs necessary. However, I'm not convinced of the correctness of that example for arbitrary G.


Mind to explain? I would like to see how you are seeing this as I am missing what you mean. :/
Sonlen wrote:
KermMartian wrote:
Indeed, that's more or less what I expected regarding the three subs necessary. However, I'm not convinced of the correctness of that example for arbitrary G.


Mind to explain? I would like to see how you are seeing this as I am missing what you mean. :/
I wasn't convinced that your code would properly deal with the case of the replacing the first or last character in the string.
UPDATE.

I dunno what to put here. Kerm said update.
Raylin wrote:
UPDATE.
I dunno what to put here. Kerm said update.
Well, mostly that you had decided to go with Weregoose's code, and your personal debate that we Skyped about regarding making it a subprogram or not.
KermMartian wrote:
Raylin wrote:
UPDATE.
I dunno what to put here. Kerm said update.
Well, mostly that you had decided to go with Weregoose's code, and your personal debate that we Skyped about regarding making it a subprogram or not.


Oh. That? Yeah. Basically, I went with Weregoose's code. On further investigation, I talked to Kerm about making the display routine a subroutine. But, the code was so small that I could get away with two or three iterations of the same code in the program.
Raylin wrote:
KermMartian wrote:
Raylin wrote:
UPDATE.
I dunno what to put here. Kerm said update.
Well, mostly that you had decided to go with Weregoose's code, and your personal debate that we Skyped about regarding making it a subprogram or not.


Oh. That? Yeah. Basically, I went with Weregoose's code. On further investigation, I talked to Kerm about making the display routine a subroutine. But, the code was so small that I could get away with two or three iterations of the same code in the program.
Indeed, and I think that'll work nicely for you. Especially since your use of a variable might make the Ans structure somewhat complex.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement