Random access to the characters in a string?

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#2
---- Original Message ----
From: vefatica
To: [email protected]
Sent: Saturday, 2011. February 19. 22:58
Subject: [Support-t-2617] Random access to the characters in a string?

| Is there an easy way to randomly access, and change, any character
| (by position) in a string?
|
| For example, change "----" to any of "*---", "-*--", "--*-", "---*".

No easier than in C - you need to extract the parts of the string to be retained (uisng @left and @right) and concatenate the three parts (retained left substring, new substring, retained right substring). There is no equivalent to the Fortran77 statement
x[3:3]='*'
(replace the substring starting at the third character and ending at the third character with the asterisk character). A good candidate for a plugin or - better yet - a new internal function! Such a new function should allow pure insertion and pure deletion, too, simply by specifying the width of the substring to be replaced or that of the replacement substring as 0, resp. BTW, it is best to treat individual characters as one of the two special cases of strings, i.e., strings of lengths 0 and 1.
--
Steve
 
#3
On Sun, 20 Feb 2011 08:41:20 -0500, Steve Fabian <> wrote:

|| For example, change "----" to any of "*---", "-*--", "--*-", "---*".
|
|No easier than in C - you need to extract the parts of the string to be retained (uisng @left and @right) and concatenate the three parts (retained left substring, new substring, retained right substring). There is no equivalent to the Fortran77 statement
| x[3:3]='*'
|(replace the substring starting at the third character and ending at the third character with the asterisk character). A good candidate for a plugin or - better yet - a new internal function! Such a new function should allow pure insertion and pure deletion, too, simply by specifying the width of the substring to be replaced or that of the replacement substring as 0, resp. BTW, it is best to treat individual characters as one of the two special cases of strings, i.e., strings of lengths 0 and 1.

Yes. I wrote a little plugin last night. In C you can just string = L'c'.
It must be many times faster than copying the left end, then the new character,
then the right end (which I did with a user-defined function).

So far the plugin is very straightforward.

Code:
v:\> do i=0 to 3 ( echo %@strchr[----,%i,*] )
*---
-*--
--*-
---*
Are there any suggestions for it?
 
#4
---- Original Message ----
From: vefatica
...
| Yes. I wrote a little plugin last night.
| In C you can just string = L'c'.
Unlike C, where you can only replace a single character without a function call, in Fortran you can replace a whole section of a string as string(a:b)='xyzw'; IIRC some versions of BASIC also have a like operator.

| It must be many times faster than copying the left end, then the new
| character,
| then the right end (which I did with a user-defined function).

Sorry, I have given you bad advice! Loop at HELP topic "f_subst.htm"! It just needs some examples..
--
Steve
 
#5
On Sun, 20 Feb 2011 18:32:40 -0500, Steve Fabian <> wrote:

|Sorry, I have given you bad advice! Loop at HELP topic "f_subst.htm"! It just needs some examples..

Thanks. That accomplishes my original goal. My plugin (now @overwrite[]), as
it stands now, does the same thing but will stop rather than lengthen string2.