* Help with Source Template Format expression
-
BEJ
- Famous
- Posts: 196
- Joined: 10 Sep 2018 17:29
- Family Historian: V7
- Location: Boston, Massachusetts, USA
- Contact:
Help with Source Template Format expression
I'm creating some source templates and, being an expression neophyte, find that I need some help. I wish to write an expression that changes all underscores [ _ ] in a particular field (or should I say format instead of field) to commas [ , ]. There must be a way, right?
Last edited by tatewise on 26 Nov 2022 12:00, edited 1 time in total.
Reason: Improved Subject
Reason: Improved Subject
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Help with Source Template Format expression
Expressions are not very good at manipulating text and there are no simple text substitution functions.
I believe you are talking about the expression used in a Source Template Definition for one of the Format definitions (such as the Footnote Format) and wish to incorporate the content of one of the Field values.
It seems that the Field value has underscore separators that you wish to replace with commas in the Format definition.
That begs the question, why use underscore characters in the Field value? Why not use commas there?
It would be quite easy to replace every existing underscore with a comma permanently. Then you could use commas in future for that Field. This approach would sidestep the complication in the Format definition.
If there is no other way then the only expression I can think of to perform such a conversion uses the =Section(...) function.
=Section( %Field%, 1, "_" ) returns the text up to the 1st underscore (or all the text if no underscore).
=Section( %Field%, 2, "_" ) returns the text between the 1st and 2nd underscore (or all text after 1st underscore).
So to insert a comma we need to test that a section of text exists separated by an underscore by using =CombineText(...).
=CombineText( ",", Section( %Field%, 2, "_" ), , ) which conditionally produces a comma if the 2nd section exists.
That needs to be compounded together for multiple sections by using =Text(...):
=Text(Section(%Field%,1,"_") . CombineText(",",Section(%Field%,2,"_"),,) . CombineText(",",Section(%Field%,3,"_"),,) . etc)
where etc repeats the CombineText(...) function for sections 4 to n depending on how many underscores may exist.
You must replace %Field% with the data reference for the Template Field and if you tell me the type and name of the Field I can provide the reference. For example, a Text field called Data has the data reference %SOUR.~TX-DATA%
I had hoped that conditional < angle brackets > could be used rather than the CombineText(...) function.
Unfortunately, when they are used the text returned by the Section(...) function is surrounded by an extra space character before and after. I think this my be a bug in FH.
I believe you are talking about the expression used in a Source Template Definition for one of the Format definitions (such as the Footnote Format) and wish to incorporate the content of one of the Field values.
It seems that the Field value has underscore separators that you wish to replace with commas in the Format definition.
That begs the question, why use underscore characters in the Field value? Why not use commas there?
It would be quite easy to replace every existing underscore with a comma permanently. Then you could use commas in future for that Field. This approach would sidestep the complication in the Format definition.
If there is no other way then the only expression I can think of to perform such a conversion uses the =Section(...) function.
=Section( %Field%, 1, "_" ) returns the text up to the 1st underscore (or all the text if no underscore).
=Section( %Field%, 2, "_" ) returns the text between the 1st and 2nd underscore (or all text after 1st underscore).
So to insert a comma we need to test that a section of text exists separated by an underscore by using =CombineText(...).
=CombineText( ",", Section( %Field%, 2, "_" ), , ) which conditionally produces a comma if the 2nd section exists.
That needs to be compounded together for multiple sections by using =Text(...):
=Text(Section(%Field%,1,"_") . CombineText(",",Section(%Field%,2,"_"),,) . CombineText(",",Section(%Field%,3,"_"),,) . etc)
where etc repeats the CombineText(...) function for sections 4 to n depending on how many underscores may exist.
You must replace %Field% with the data reference for the Template Field and if you tell me the type and name of the Field I can provide the reference. For example, a Text field called Data has the data reference %SOUR.~TX-DATA%
I had hoped that conditional < angle brackets > could be used rather than the CombineText(...) function.
Unfortunately, when they are used the text returned by the Section(...) function is surrounded by an extra space character before and after. I think this my be a bug in FH.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
-
BEJ
- Famous
- Posts: 196
- Joined: 10 Sep 2018 17:29
- Family Historian: V7
- Location: Boston, Massachusetts, USA
- Contact:
Re: Help with an expression?
Ah ha! Yes, from what little I know about the topic I thought there MIGHT be a solution. Thank you for your thoughtful reply; you got the gist of my question. I will respond more thoroughly after I ponder and experiment. Thanks, again.
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Help with Source Template Format expression
After a little more experimentation I've found a simpler expression that solves your translation dilemma:
<{=Section(%Field%,1,"_")},{=Section(%Field%,2,"_")},{=Section(%Field%,3,"_")},{=Section(%Field%,4,"_")}>
Just keep adding ,{=Section(%Field%,n,"_")} items for n=5 - 9 before the last > and it will cope with many underscores.
This is a 'magic' feature of < angle bracket > field lists as explained in the FH Help page Source Template Formats under 'Multiple Field Codes and Separators'.
<{=Section(%Field%,1,"_")},{=Section(%Field%,2,"_")},{=Section(%Field%,3,"_")},{=Section(%Field%,4,"_")}>
Just keep adding ,{=Section(%Field%,n,"_")} items for n=5 - 9 before the last > and it will cope with many underscores.
This is a 'magic' feature of < angle bracket > field lists as explained in the FH Help page Source Template Formats under 'Multiple Field Codes and Separators'.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
-
BEJ
- Famous
- Posts: 196
- Joined: 10 Sep 2018 17:29
- Family Historian: V7
- Location: Boston, Massachusetts, USA
- Contact:
Re: Help with Source Template Format expression
In responding I find that I have a fundamental gap in my understanding of Source records. So, before proceeding, I've posted a new question here: Understanding source templates (21211)
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Help with Source Template Format expression
Further experimentation provides an even clearer expression provided that each underscore is followed by a space:
{=Section(%Field%,1,"_ ")}, {=Section(%Field%,2,"_ ")}, {=Section(%Field%,3,"_ ")}, {=Section(%Field%,4,"_")}
Just keep adding , {=Section(%Field%,n,"_ ")} items for n=5 - 9 on the right and it will cope with many underscores.
This stems from the Help page Source Template Formats under 'Special-Handling for Top-Level Text' regarding spaces between items.
{=Section(%Field%,1,"_ ")}, {=Section(%Field%,2,"_ ")}, {=Section(%Field%,3,"_ ")}, {=Section(%Field%,4,"_")}
Just keep adding , {=Section(%Field%,n,"_ ")} items for n=5 - 9 on the right and it will cope with many underscores.
This stems from the Help page Source Template Formats under 'Special-Handling for Top-Level Text' regarding spaces between items.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
-
BEJ
- Famous
- Posts: 196
- Joined: 10 Sep 2018 17:29
- Family Historian: V7
- Location: Boston, Massachusetts, USA
- Contact:
Re: Help with Source Template Format expression
Than for the extra effort, Mike. I have not had a chance to play with this yet. Will let you know how it goes.