Writing Grep matches into a global string variable
Peeyush Khare
Hello,
Curious if there is a way to write several strings into one global string variable. For example, I have a 1D text wave with chemical formulas. I need to pull formulas that contain [15N] and store them all together in a global string, each formula separated by a ";". I was trying to do something like this:
// labelindex is the wave containing index numbers of cells containing [15N] in the chemical formulas.
// labelindex is created using Grep.
variable count = dimsize(labelindex,0)
for(i=0;i<count;i+=1) //run through the text wave
string name
sprintf name,"%s", textwave[labelindex[i]] //textwave contains all the chemical formulas
print name
name = replacestring("[",name,"/[") //to avoid regex acting up on "[" or "]"
name = replacestring("]",name,"/]")
SNRcolornames = name //SNRcolornames is the global string
endfor
// labelindex is created using Grep.
variable count = dimsize(labelindex,0)
for(i=0;i<count;i+=1) //run through the text wave
string name
sprintf name,"%s", textwave[labelindex[i]] //textwave contains all the chemical formulas
print name
name = replacestring("[",name,"/[") //to avoid regex acting up on "[" or "]"
name = replacestring("]",name,"/]")
SNRcolornames = name //SNRcolornames is the global string
endfor
I know this is not accurate yet because the end result of this is SNRcolornames with only one string recorded in it, which is the last match in the textwave.
How do I make SNRcolornames store all matches separated by a ";"?
Thanks for the help!
Sincerely,
Peeyush
Is AddListItem your friend here?
DisplayHelpTopic("AddListItem")
See also the other related functions to deal with string lists.
November 23, 2023 at 03:20 am - Permalink
Beautiful solution! and friend it is, indeed. Thanks KurtB! I'll check out other functions too.
November 23, 2023 at 03:40 am - Permalink
string strList = ""
Extract/free textwave, wmatches, GrepString(textwave, regex)
wfprintf strList, "%s;", wmatches
return strList
end
November 23, 2023 at 04:55 am - Permalink
Thanks, Tony! I was able to work with GrepString for a different problem I was trying to tackle.
November 30, 2023 at 08:03 am - Permalink