Tagdata() and Settagdata

I am attempting to understand how tagdata() and settagdata() work. I see from the _TextLib “Settagdata” code that it is using “search” to locate the position in the text of the first occurrence of a tag. Then the code is

newPage=thePage[1,ts-1]+prefix+parameter(4)+suffix+thePage[ts+te,-1]

A few questions
Can someone explain what the code is doing?
How does this code handle more than 1 occurrence of a tag with the same name?
Is a similar code used to display a tag value?
Where can I find the code for “Search”?

Thank you.

It is creating the new text by piecing together the following:

thePage[1,ts-1]

// The text funnel goes from the first character to just before the tag prefix. The ts-1 would be the position of the first character of the full tag less 1.

+prefix+parameter(4)+suffix

// Adds the original prefix plus the new tagdata value plus the original suffix.

 +thePage[ts+te,-1]

// Here ts is the start of the tag prefix and te is the full length of the tag prefix, data and suffix. This adds on the remainder of the original text.

It will only get the first occurrence.[quote=“goMiddleton, post:1, topic:1662”]
Is a similar code used to display a tag value?
Where can I find the code for “Search”?
[/quote]

I believe these are all hard coded functions so there is no way to investigate them.

Actually, it looks like tagdata( is a custom function.

TAGDATA+4(array(array(regexarray(•1,regexliteral(•2)+"(.*?)"+regexliteral(•3),chr(1)+"1"+chr(1),chr(1)+"2"+chr(1)),•4,chr(1)+"1"+chr(1)),2,chr(1)+"2"+chr(1))

but I expect it would be simpler to just read the documentation for tagdata(, than it would be to figure it out from the documentation for regexarray( and regexliteral(.

Thinking about this I whipped up some simple code that would change the data in all sets of the tags found in the text. The parameters would be the same as in settagdata but I would call this version setalltagdata.

 // setalltagdata PAGE, PREFIX, SUFFIX, DATA, SEP

 local thePage,prefix,suffix,tagList,newData,tsep
 thePage=parameter(1)
 prefix=parameter(2)
 suffix=parameter(3)
 newData=parameter(4)
 tsep=parameter(5)
 tagList=tagarray(thePage,prefix,suffix,cr())
 If tagList=""
     If tsep<>""
         thePage=thePage+tsep+prefix+newData+suffix
     else
         thePage=thePage
     endif 
 Else
     looparray tagList,cr(),element,index
         thePage=replace(thePage,prefix+element+suffix,prefix+newData+suffix)
     endloop
 endif
setparameter 1,thePage

This could be made a custom statement or used as is as a standalone procedure activated by a call statement using the proper parameters:

call setalltagdata,theText,"<NAME>","</NAME>","Joe Blow",cr()

This would change data in all occurrences of the tags to “Joe Blow” or add a tag at the end of theText with that data if there were no tags in the original text. I don’t know how often a need for this would arise so it might be a little wasted effort on my part - not the first time for sure.

Hi Gary,

It will take me some time to study that and try to figure out what is doing what. Thank you, although as you guessed I don’t currently need to change all of the occurrences. I do however need to pick one and change it. An example is if the first occurrence needed to be edited as well as the 3rd occurrence. I can get the values of those with tagdata() but I don’t think there is a way currently to change a specific occurrence. I am interested in learning what you did because maybe it could be adapted to target a specific occurrence.

Thank you Gary

Steve, my code is really rather simple when you get right down to it. First a list of the contents of all the matching tags is compiled into the tagList variable. If there was a separator character entered in the 5th parameter and this tagList is empty (no matching tags exist in the text) then we add a new tag at the end of the text with the value given in the 4th parameter. If the tagList is empty and there is no 5th parameter given then we leave the original text unchanged.

If none of the above are true we then take our list of the existing tag values and run a looparray from the tagList variable and use the replace( function to replace each existing tag (including the header, value and trailer) with the new tag string. As I said, fairly easy stuff.