[Thread Prev][Thread Next][Index]

[ferret_users] string substitution in array



Dear Ferrets,

I would like to substitute substrings in an array of strings.  For a
single string this is easy to do.  Say I want to replace "old" for "new"
in the following example:

let test = "abc_old_xyz"
let myInd = StrIndex( test, "old" )
list SubString( test, 1, `myInd-1` ) + "new" + SubString( test, `myInd+3`, StrLen(test) )

I want to do the same for an arbitrary array (for readability I leave
out the option of arbitrary strings or substrings):

let strArray = XSequence( {"a_old", "bb_old", "ccccc_old"} )
let newArray = SubString(strArray, 1, 2) + "new"

This works, but suppose I have an array with the locations of the
character just before 'old' that I want to replace, then this comes
closer:

let myInd = XSequence( {2, 3, 6} )
let newArray = SubString(strArray, 1, myInd) + "new"

Of course, I'd like to search for the string that I want to replace, so
I try:

let myInd = StrIndex( strArray, "old" )
let newArray = SubString(strArray, 1, myInd) + "new"

We need to shift the substring to the left, so:

let newArray = SubString(strArray, 1, myInd-1) + "new"

This works.  It is left as an exercise to the reader what to do when
there are characters following the replacement.

One may think that this is easier if you spawn the sed(1) command, as 
that does not require index manipulation:

let test = "abc_old_xyz"
list SPAWN( "echo `test` | sed '{s/old/new/}'" )

With an array of strings it becomes like this:

let strArray = XSequence( {"a_old", "bb_old", "ccccc_old"} )
let newArray = SPAWN( "echo `strArray` | sed '{s/old/new/}'" )

That doesn't work, since strArray is an array while grave accents want
to evaluate it to a scalar.  I tried it in a loop, and by use of
symbols (which are evaluated even earlier than grave accents), but I
cannot figure it out.  So, in this case, while string manipulation may
be a bit circuitous in Ferret, it is a method that works.  But please
let me know if you have a better solution.

Marco

-- 
Laboratoire des Sciences du Climat et l'Environnement (LSCE)
Tel: +33 1 6908 3876
http://www.lsce.ipsl.fr/Pisp/marco.van-hulten/


[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement