Page 1 of 1

Text Scheme Expression for Occupation

Posted: 24 Mar 2023 03:22
by sbell95
Hi all,

I am wanting to include the first (or preferred) occupation of an individual on a diagram using my custom text scheme. However, in instances where an individual's first occupation is that of "Scholar" (ie, because of censuses), I want the text scheme to show either the next occupation that is not "Scholar" or my fact-flagged preferred occupation. Or perhaps a blank "No Occupation" in the case of there being no subsequent occupation facts.

I tried to implement the following expression in my text scheme, but it doesn't seem to work...

Code: Select all

=CombineText("(",ItemIf(%INDI.OCCU[1]% = "Scholar",ForceText("(No Occupation)"),%INDI.OCCU[preferred]%),")","")
Can anyone advise?

Sarah

Re: Text Scheme Expression for Occupation

Posted: 24 Mar 2023 11:08
by tatewise
When you say it doesn't seem to work it would help to explain what does not work.
Does it display nothing, display the wrong Occupation, display the expression, or what?

Let us investigate the expression components.

ItemIf( %INDI.OCCU[1]% = "Scholar", ForceText("(No Occupation)"), %INDI.OCCU[preferred]% )
ItemIf(...) is inappropriate as the parameters display text, so TextIf(...) is required.
ForceText(...) is redundant in this context as it is not governed by a subsequent data reference.
So we get:
TextIf( %INDI.OCCU[1]% = "Scholar", "(No Occupation)", %INDI.OCCU[preferred]% )
If the 1st Occupation is "Scholar" then that function displays "(No Occupation)" otherwise it displays the preferred Occupation or the 1st Occupation or nothing if neither of those exists.
So that does not match what your description specifies.

The =CombineText( "(", ... , ")", "" ) function surrounds the TextIf(...) display with brackets except when it is nothing.
So you get for example:
((No Occupation)) I don't think you really want double brackets?
(teacher)

I suggest you start with the =TextIf(...) function on its own in the Text Scheme and try to get nearer to your specification rather than me simply providing the solution. Remember to use the Test button in the Edit Text Scheme Item dialogue to check the syntax is valid.

Re: Text Scheme Expression for Occupation

Posted: 24 Mar 2023 12:08
by sbell95
Thank you, Mike. I think I've wrapped my head around it now -- I was overthinking the use of the =CombineText function.

My solution is now simply:

Code: Select all

(=TextIf(%INDI.OCCU% = "Scholar",%INDI.OCCU[2]%,%INDI.OCCU[preferred]%))
I also have an alt. text scheme item which just outputs "(No Occupation)" if the previous item is blank -- but I am unsure if the two items could be combined?

As an aside, the index [1] seems to get deleted from the first %INDI.OCCU% when I close/reopen the text scheme editor dialog -- is that the intended behaviour?

An example screenshot of my diagram with the above applied, in case anyone is interested:
Screenshot 2023-03-24 230529.png
Screenshot 2023-03-24 230529.png (205.25 KiB) Viewed 746 times

Re: Text Scheme Expression for Occupation

Posted: 24 Mar 2023 12:56
by tatewise
Yes, that is much nearer your stated requirement.

You don't need another Text Scheme Item, just use the 'No Data' Text: tick box and enter (No Occupation) there in the same item as your Template expression.

The index [1] is optional in any data reference as that is the default if no index is supplied and some Templates remove the [1] index to save space.

There is one case where your current expression may not perfectly match your stated requirement.
If the 1st Occupation is 'Scholar' it displays the 2nd Occupation even if the 3rd or later Occupation is Preferred.
You may need nested TextIf(...) functions to resolve that case.

BTW: Do you need to cope with both 'Scholar' and 'scholar' or any other case variant?

Re: Text Scheme Expression for Occupation

Posted: 24 Mar 2023 14:05
by tatewise
I think this is the full expression you need and rather simpler than I anticipated:

Code: Select all

(=TextIf( ToLower(%INDI.OCCU[preferred]%) = "scholar", %INDI.OCCU[2]%, %INDI.OCCU[preferred]%))
i.e.
If no Occupation is preferred then the condition is testing %INDI.OCCU[1]% which is just like your original expression.
If an Occupation is preferred then presumably it will never be Scholar so %INDI.OCCU[preferred]% is always displayed.

Re: Text Scheme Expression for Occupation

Posted: 25 Mar 2023 00:12
by sbell95
You don't need another Text Scheme Item, just use the 'No Data' Text: tick box and enter (No Occupation) there in the same item as your Template expression.
Of course! So obvious once you mentioned it.
BTW: Do you need to cope with both 'Scholar' and 'scholar' or any other case variant?
I use consistent occupation titles, so all of them will be "Scholar".

Code: Select all

(=TextIf( ToLower(%INDI.OCCU[preferred]%) = "scholar", %INDI.OCCU[2]%, %INDI.OCCU[preferred]%))
What role does the ToLower function play here? And I'm guessing the first parameter uses %INDI.OCCU[preferred]% because FH takes the first occupation fact as preferred (unless otherwise set by a fact flag) -- is that right?

Re: Text Scheme Expression for Occupation

Posted: 25 Mar 2023 08:57
by laz_gen
BTW: Do you need to cope with both 'Scholar' and 'scholar' or any other case variant?
I think it is meant to cope with different uses of upper case so a comparison of Scholar to scholar doesn't fail.

It converts all text found in the OCCU fact to lower case before doing its comparison looking for scholar.

Re: Text Scheme Expression for Occupation

Posted: 25 Mar 2023 10:29
by tatewise
The purpose of the ToLower(...) function is given in the FH Help for Functions pages. See ToLower page.
As laz-gen says, it converts the Occupation to lowercase text for comparison with "scholar" so it is case-independent.

Yes, if there is no Preferred Occupation then the 1st Occupation is used.
See Understanding Data References under Advanced Indices where it says:
It is possible to set a flag on events and attributes to indicate that a fact is a preferred fact. For example, you may have recorded numerous occupations for a given person, but one of them may be the occupation that they were most closely associated with; and you may choose to mark this occupation as the preferred one, by setting the Preferred flag for that fact. You can reference a preferred fact by using [preferred] as the index. e.g.:

%INDI.OCCU[preferred]%

If a given person has no occupation which has the Preferred flag set, this will return the first one.

Re: Text Scheme Expression for Occupation

Posted: 25 Mar 2023 23:42
by sbell95
I know what the funcion does; I was just wondering why it was required here. Both you and laz_gen have now answered that, so thank you.

If I am consistent throughout my project in recording the occupation as "Scholar" (rather than "scholar" or "sChOlar"), can my text scheme expression simply be closer to what I originally suggested?

Code: Select all

(=TextIf(%INDI.OCCU[preferred]%) = "Scholar", %INDI.OCCU[2]%, %INDI.OCCU[preferred]%)

Re: Text Scheme Expression for Occupation

Posted: 26 Mar 2023 10:21
by tatewise
Yes, but you have not got all the closing brackets ) in the correct positions.