Page 1 of 1

double quotes

Posted: Fri May 10, 2013 9:31 pm
by dantomlin
I need to add double quotes to each item in a variable. I have 183 fields in a variable and I need to create a file with each field enclosed in quotes in order to import into another system.

I have been trying a repeat loop but having trouble getting the quotes around each item in the variable...

Any suggestions would be greatly appreciated....

Thanks, Dan

Re: double quotes

Posted: Fri May 10, 2013 9:44 pm
by FourthWorld
Post the code you have and we can revise it.

Re: double quotes

Posted: Fri May 10, 2013 10:17 pm
by shaosean

Code: Select all

function addQuotes pDataIn
  local tDataOut
  repeat for each line tLine in pDataIn
    put QUOTE & tLine & QUOTE & LF after tDataOut
  end repeat
  if (char -1 of tDataOut = LF) then
    delete char -1 of tDataOut
  end if
  return tDataOut
end addQuotes

Re: double quotes

Posted: Fri May 10, 2013 11:11 pm
by dunbarx
What Shaosean said.

What you may have stumbled over is that since quotes are used intimately in the language, to define literals, for example, the engine will not simply add them just because you wrote them. For example, if you write"

put "foo" into myVariable -- you will find foo, not "foo"

What you want is to use the quote constant (see the dictionary):

put quote & "foo" & quote into myVariable --now you will find "foo"

So it is important to see that with such a highly integrated native element as a double quote, which is used to help the engine parse its code, the constant "quote" (no joke intended) is provided to explicitly add a double quote to a string. There are other constants that fall into the same category, though probably not so egregiously.

Craig Newman

Re: double quotes

Posted: Sat May 25, 2013 1:53 pm
by andrewferguson
This code might be what you are wanting.

Code: Select all

   --This Code puts quotes around all the words from the variable input, and places the end result into the variable output
   global input, output
   put empty into output
   put 1 into counter
   repeat for the number of words of input
      put quote & word counter of input & quote & space after output
      add 1 to counter
   end repeat
Andrew

Re: double quotes

Posted: Tue May 28, 2013 5:43 pm
by dantomlin
thanks.. I will give it a try.

Re: double quotes

Posted: Tue Oct 06, 2015 7:43 pm
by mrcoollion
Because i got tired of adding double quotes to each item in a variable and because sometimes it gives me trouble I use the following very simple method that only needs one additional script-line.
Place the following in a button script and you will see how easy it is.

Code: Select all

on mouseUp
   put "This is @Item01@  and this is @Item02@"  into theString 
   replace "@" with Quote in theString
   answer theString
end mouseUp
Hope this makes it somewhat easier for some of us.

And of-course you can replace the @ for anything you like as long as it is not a part of the original string ( eg #q# or anything else).

Kind regards,

Paul

Re: double quotes

Posted: Tue Oct 06, 2015 8:20 pm
by dunbarx
Hi.

Nothing like building your own toolbox of goodies. One sometimes spends more effort on these than on the project itself. But think about this. You need a button and a field. In the button script:

Code: Select all

on mouseUp
   put "ABC" into temp
   answer temp --just the literal
   put addQuotes(temp) into fld 1 --quotes added to temp
end mouseUp

function addquotes var
   return quote & var & quote
end addquotes
The function "addQuotes" can be placed in the hierarchy, and you can then use it wherever you please.

Craig

Re: double quotes

Posted: Wed Oct 07, 2015 4:57 am
by Simon
Do I get to post this again?
http://forums.livecode.com/viewtopic.ph ... 73#p100173

Ohh too late to stop me... :)
Klaus has a different method as well.

Simon
ooops; same problem different situation.