I was on auto-pilot the other day, doing Yet Another Middle + Position text-parsing operation.
Middle is a common tool, you see it in Excel and other places as well as FileMaker; the structure is Middle(Text, Start, Size), so you have those three clauses with Text being a field or an expression that reconciles to be a blob of text and then Start and Size being numbers — how many characters deep into Text to start from and how many characters from that point onward to include.
Position is Position (Text, String, Start, Occurrence) and returns a number for how many characters deep into Text the String is found if you start off at Start and count to the Occurrence (first, second, nineteenth, whatever).
Simple, no?
I do this all the time. There are two obvious variants ‚ the one for snagging a single value (e.g., the first value in the set) and the one for walking through each value in turn within a looping script. Here’s the simple single-value formulation:
Set Variable [$Values2Parse, “¶” & ValueListItems(Get(FileName), “RelatedLineItems”& “¶”)
Set Variable [$Value_I_Want, Middle($Values2Parse, Position($Values2Parse, “¶”, 1, 1)+1, Position($Values2Parse, “¶”, 1, 2)-Position($Values2Parse, “¶”, 1, 1)-1)
And here’s the slightly different formulation for walking through values in a loop:
Set Variable [$Values2Parse, “¶” & ValueListItems(Get(FileName), “RelatedLineItems”& “¶”)
Set Variable [$Pos, 1]
Loop
Exit Loop If [$Pos + 1 > PatternCount ($Values2Parse, “¶”)]
Set Variable [$Value_I_Want, Middle($Values2Parse, Position($Values2Parse, “¶”, 1, $Pos)+1, Position($Values2Parse, “¶”, 1, $Pos+1) - Position($Values2Parse, “¶”, 1, $Pos) - 1)]
<do stuff with that value>
Set Variable [$Pos, $Pos + 1]
End Loop
So day before yesterday I spent 40 minutes trying to figure out why the values I was getting out of one of those looping-script versions was all bollixed up. Kept opening the script and looking at it. Looked totally normal. Here’s what I saw, or rather looked at but didn’t see:
Set Variable [$Value_I_Want, Middle($Values2Parse, Position($Values2Parse, “¶”, 1, $Pos)+1, Position($Values2Parse, “¶”, 1, $Pos+1) - Position($Values2Parse, “¶”, 1, 1) - 1)]
Well, folks… both versions work fine in their respective contexts, but a hybrid of the two…yeesh! :smack:
You get to the point you can write them without having to think about them, then you quit thinking about what you’re doing when you write them, and also don’t really think about the meaning of what you’re looking at (“yeah, that looks normal, so what’s wrong?”). Bad autopilot brain, bad!