Most streamlined method to get data

Ben Rubinstein benr_mc at cogapp.com
Thu Jul 29 12:24:17 EDT 2021


As grepophile, I'd go for some variation on

	get matchText(last line of myVar, \
		",+Total,.*\"\$(\d*,?\d+\.\d\d)\",+", \
		myTotal)

obviously details depend on what variations you expect within the data.


Note that there's a nasty aspect of LC parsing: in order to have quotation 
marks inside your string (the pattern) you have to precede them with a backslash.

So the pattern is really:
	,+Total,.*"\$(\d*,?\d+\.\d\d)",+

but to represent it as a string in the script, you have to escape the internal 
quotes
	",+Total,.*\"\$(\d*,?\d+\.\d\d)\",+"

But the version of the parser that deals with colouring and indentation in the 
script editor doesn't know about this, so it colours it funny, and indents the 
next line in the script wrong; and in some circumstances gives you a spurious 
syntax error (you can get round that by just adding a blank line).

So much so that the single line of code I quoted above works. But when I tried 
to make it neater:

	put ",+Total,.*\"\$(\d*,?\d+\.\d\d)x\",+" into kPattern
          -- blank line
	get matchText(myVar, kPattern, myTotal)

It doesn't work: kPattern is actually an empty string. I don't know why. So 
the parser allows \" inside a string within a function call, but misinterprets 
it in a variable assignment.

When I do this sort of thing now I often use format to make it neater - with a 
continuation backslash for the second line, which fixes the indentation 
problem). But there's a gotcha:

    put format(",+Total,.*\"\\$(\\d*,?\\d+\\.\\d\\d)\",+" \
          ) into kPattern
    get matchText(last line of fld "data", kPattern, tFine)

The basic string parsing interprets \" as quote (except when it doesn't); but 
doesn't attempt to interpret the rest of the slashes. But when a string is 
entered as a parameter to format, format interprets slash as a general escape 
character (so that, e.g. "\t" can be transformed to tab). So when we actually 
need a slash in the string, in order to represent grep items like "\d", we 
have to use two slashes before the string gets to format.

All this makes it harder to read the grep pattern. And grep patterns are often 
hard enough to read already. But I love them.

Ben


On 29/07/2021 13:42, Skip Kimpel via use-livecode wrote:
> I have a variable that contains:
> 
> ,Name,,,Quantity,,,,,Total,,,Percent
> 
> ,Dine In,,,189,,,,,"$4,812.71",,,57.96%
> 
> ,Take Out,,,72,,,,,"$1,676.43",,,20.19%
> 
> ,3rd Party,,,54,,,,,"$1,779.35",,,21.43%
> 
> ,Bakery,,,3,,,,,$34.83,,,0.42%
> 
> ,Total,,,318,,,,,"$8,303.32",,,
> 
> 
> What is the best way to get 8303.32 from that last line.  Obviously, the
> complexity is the commas and getting rid of the dollar sign as well.
> 
> 
> I know I am going to kick myself when somebody answers.... I am blank this
> morning.  (NEED MORE COFFEE!!!!!)
> 
> 
> SKIP
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
> 




More information about the use-livecode mailing list