Embedded Quotes Revisted

Ken Ray kray at sonsothunder.com
Mon May 17 16:13:40 EDT 2004


> I searched the archives and found one answer that involved 
> CLIPS (?) and using a slash; the other was something 
> involving using a function from Ken Ray which I don't really 
> understand:
> 
> ========================================
> answer "She said, " & q("I do.")
> 
> function q what
>   return quote & what & quote
> end q
> =======================================
> 
> Is there anyone who can translate this from geekSpeak to 
> normalHumanSpeak? (sorry; I'm no programmer)

Judy,

Since I'm the one that wrote the q() function I thought it would be
appropriate for me to respond. As you've discovered, sometimes Rev is very
flexible about the use of quotation marks when referring to different
things, and sometimes it's not.

For example, if you had a field named "Data", you could script:

  put 10 into fld Data

and you'd get the number 10 in the field. This is Rev's method of
interpretation... if something *looks like a variable*, it is first
evaluated to see if it *is* a variable. If not, it looks to see if there's
an object by that name. So whereas this:

  put 10 into fld Data

should work, this won't:

  put 50 into Data
  put 10 into fld Data

Because by the time it gets to the phrase "fld Data", Data is now a variable
containing the value of "50", so Rev interprets this as:

  put 10 into fld 50

And if you don't have 50 fields on your card, you'll get an error. However,
when you *use* quotes, all the ambiguity disappears... Rev knows to look for
an object named appropriately, so:

  put 10 into fld "Data" 

will always attempt to find the field named "Data" so that 10 can be put
into it.

My q() function is a shorthand way of adding surrounding quotations marks to
a string to help aid in resolving these kind of ambiguities, and to be used
in circumstances where you can't have multiple quotes in a string. The
example I provided:

  answer "She said, " & q("I do.")

would bring up an answer dialog with the string:

  She said, "I do."

In order to get the quotes around "I do.", you can't just do this:

  answer "She said, "I do.""

because Rev will display an error as it doesn't understand the "internal"
quotes. No you *could* do:

  answer "She said, 'I do.'"

but you wouldn't get double quotes around the phrase "I do.", you'd get
single quotes. So in order to provide those quotes around the phrase, you
need to either provide the constants for them, as in:

  answer "She said, " & quote & "I do." & quote

or use a function like q() which takes a non-quoted string and returns it
with quotes around it, as in:

  answer "She said, " & q("I do.")
  
  function q what
    return quote & what & quote
  end q

or you can use the format() function in Rev which does the same thing but
requires that you *escape* the quote by preceding it with a backslash (\),
as in:

  answer format("She said, \"I do.\"")

Escaping the quote makes sure that Rev doesn't choke on the "extra"
quotation marks, but properly understands how to implement them.

So when you're building your scripts, suppose the user selects "mouseUp"
from Button #1, and then (since I don't really know what's in Button #2, I'm
making it up) selects "answer" from Button #2 and is asked via an ask dialog
what they want to answer. Let's suppose they type:

  Hello there

into the ask dialog and click OK, and you want to make sure the assembled
script ends up looking like this:

  on mouseUp
    answer "Hello there"
  end mouseUp

So if we assume that the script is assmbled as they go, and after they've
chosen and responded to the "answer" selection that the "end mouseUp" is
automatically added, the scripts are like this:

-- Script of dropdown #1
on menuPick pChoice
  -- pChoice is something like "mouseUp"
  put "on" && pChoice into fld "Script"

  -- Then store that choice so we can automatically end the handler
  -- after selecting from dropdown #2
  set the uSelectedChoice of this card to pChoice
end menuPick

-- Script of dropdown #2
on menuPick pChoice
  if pChoice = "answer" then
    ask "What do you want to answer:"
    if it is not empty then
      put "answer" && quote & it & quote into line 2 of fld "Script"
      put "end" && (the uSelectedChoice of this card) into line 3 of fld
"Script"
    end if
  else
    -- handle other choices here
  end if
end menuPick


HTH,

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/







More information about the use-livecode mailing list