From: ·········@sriaurobindoashram.com
Subject: sending a format result to a string variable
Date: 
Message-ID: <1128615580.903209.88040@f14g2000cwb.googlegroups.com>
Dear Lispers,
In a web app. I have a result list of lists from a sql query and wish
to pass the data to a Java Script array for manipulation in the
browser. Here is my code to format that list and a setq form that gives
an example of the list itself.

My code (taken from Practical Common Lisp) works fine for printing on
the screen but not for putting the result into a variable either with
setf or setq. Can you help get the result string into a variable??
I couldn't understand passing the format result to a string with a fill
pointer. How to create a string with a fill pointer and use it for
passing the format result??
If you have any better ideas on geting sql results into a JavaScript
array send them.
                   Thanks so much, Markandeya

(format t "'" (loop for cons on P
    do (format t "~{~^ ~^'~a~^',~}" (car cons))
    when (cdr cons) do (format t ",")))

(setq P '((Sarojini Grace 1) (Hari Om 2) (Mira Shanti 3)))

From: Aaron Sokoloski
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1128617589.416420.206830@g14g2000cwa.googlegroups.com>
Try the "with-output-to-string" macro.
From: Will McCutchen
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1128617737.316150.98430@z14g2000cwz.googlegroups.com>
> My code (taken from Practical Common Lisp) works fine for printing on
> the screen but not for putting the result into a variable either with
> setf or setq. Can you help get the result string into a variable??

I'm new to this whole Lisp thing, so this might not be the best way to
do it, but it seems to work for me:

(with-output-to-string (s)
  (format s "'" (loop for cons on P
                      do (format s "~{~^ ~^'~a~^',~}" (car cons))
                      when (cdr cons) do (format s ","))))


Hope that helps.


Will.
From: Peter Seibel
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <m2u0fu37kt.fsf@gigamonkeys.com>
·········@sriaurobindoashram.com writes:

> Dear Lispers,

> In a web app. I have a result list of lists from a sql query and wish
> to pass the data to a Java Script array for manipulation in the
> browser. Here is my code to format that list and a setq form that gives
> an example of the list itself.
>
> My code (taken from Practical Common Lisp) works fine for printing on
> the screen but not for putting the result into a variable either with
> setf or setq. Can you help get the result string into a variable??
> I couldn't understand passing the format result to a string with a fill
> pointer. How to create a string with a fill pointer and use it for
> passing the format result??

In this case you don't need to screw around with strings with fill
pointers since you can generate the result with a single call to
FORMAT and use NIL as the first argument, causing FORMAT to return a
string:

  CL-USER> (format nil "~{~{'~a'~^, ~}~^, ~}" p)
  "'SAROJINI', 'GRACE', '1', 'HARI', 'OM', '2', 'MIRA', 'SHANTI', '3'"

> If you have any better ideas on geting sql results into a JavaScript
> array send them.

Though presumably if you want to generate a string that you can
include in a web page as a Javascript array you'll need some brackets:

  CL-USER> (format nil "[~{~{'~a'~^, ~}~^, ~}]" p)
  "['SAROJINI', 'GRACE', '1', 'HARI', 'OM', '2', 'MIRA', 'SHANTI', '3']"

If you need to generate strings that can't be handled by a single call
to FORMAT you should look up WITH-OUTPUT-TO-STRING (which I may not
have discussed in the book.)

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Christopher C. Stacy
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <uk6gqa6mo.fsf@news.dtpq.com>
·········@sriaurobindoashram.com writes:

> My code (taken from Practical Common Lisp) works fine
> for printing on the screen but not for putting the
> result into a variable either with setf or setq. 
> Can you help get the result string into a variable??
> I couldn't understand passing the format result to a
> string with a fill pointer. How to create a string
> with a fill pointer and use it for passing the format
> result??  If you have any better ideas on geting sql
> results into a JavaScript array send them.

> (format t "'" (loop for cons on P
>     do (format t "~{~^ ~^'~a~^',~}" (car cons))
>     when (cdr cons) do (format t ",")))
> 
> (setq P '((Sarojini Grace 1) (Hari Om 2) (Mira Shanti 3)))

Markandeya,

Your above program above has a bug: the last item 
(the number) in each tuple doesn't get a closing
single-quote.

I would write it by using FORMAT to do all the work of
picking it apart, without writing the loops and CAR/CDR.

(format nil "~{~{'~A'~^, ~}~^, ~}" p)

=> "'SAROJINI', 'GRACE', '1', 'HARI', 'OM', '2', 'MIRA', 'SHANTI', '3'"

If that FORMAT control string looks really baroque, 
note that it's just one trick, used twice (nested).
You only need to grasp this:   ~{'~A'~^, ~}

If you just want a single FORMAT call to return a string
result, rather than printing, give it NIL as the output
destination.

When you have several output calls and want it all to end 
up in one string, I would suggest WITH-OUTPUT-TO-STRING.

If for some reason you really need to explicitly make a
string with a fill pointer, see MAKE-ARRAY.  But the above
ideas are more likely to be what you want.

Also, Common Lisp is case-insensitive, so I would not
used mixed case in my code.  Lowercase is how most
people write Lisp code today.  (It gets converted
to all-uppercase internally, which is what you will
see when you are in the debugger).

Have fun!
From: drewc
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <VYf1f.97471$oW2.1003@pd7tw1no>
·········@sriaurobindoashram.com wrote:
> Dear Lispers,
> In a web app. I have a result list of lists from a sql query and wish
> to pass the data to a Java Script array for manipulation in the
> browser. Here is my code to format that list and a setq form that gives
> an example of the list itself.
> 
> My code (taken from Practical Common Lisp) works fine for printing on
> the screen but not for putting the result into a variable either with
> setf or setq. Can you help get the result string into a variable??
> I couldn't understand passing the format result to a string with a fill
> pointer. How to create a string with a fill pointer and use it for
> passing the format result??
> If you have any better ideas on geting sql results into a JavaScript
> array send them.
>                    Thanks so much, Markandeya

I suggest that, if you want to generate JavaScript from within CL 
(something i do all the time), you take a look at parenscript. Below is 
the code i use to pass a Javascript array (via AJAX) to a combo-box 
widget :

(defmethod render-search-results ((self dojo-combo-box))
   (with-slots (list-of-values values-generator render-function)
       self
     (setf list-of-values (funcall values-generator self))
     (<:as-html  (js:js* `(array
			  ,@(loop for v in list-of-values
				  for c from 1
				  collect `(array ,(funcall render-function v) ,c)))))))

which will create a JS array that looks like :
"[ ["foo", 1] , ["bar", 2] ]".

My rule of thumb is that, if i find myself generating code with format 
and/or w-o-t-s , it's time for a macro :)

hth.

drewc


-- 
Drew Crampsie
drewc at tech dot coop
  "... the most advanced use of lisp in the field of bass lure sales"
	-- Xach on #lisp
From: markandeya
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1128659252.473421.159300@g43g2000cwa.googlegroups.com>
Wow!! You guys are great! Thanks so much for the quick replies. i will
work through them today. It is so nice having a group of backup
support, i don't feel so lost in space. Esp. thanks Peter for taking
the time and care to reply, your book has been a great assistance in
getting me going in Lisp. Of course i still think like a programmer in
another language and not like a Lisp programmer but that will take some
time and experience with the language. Thanks again to all of you who
replied, i read each one and gain insight from every one. Markandeya
From: ··············@hotmail.com
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1128664260.240816.277140@g43g2000cwa.googlegroups.com>
·········@sriaurobindoashram.com wrote:
>
> (format t "'" (loop for cons on P
>     do (format t "~{~^ ~^'~a~^',~}" (car cons))
>     when (cdr cons) do (format t ",")))
>
> (setq P '((Sarojini Grace 1) (Hari Om 2) (Mira Shanti 3)))

Some other folks have pointed you in the right direction.

One thing *really* bothers me about this code snippet, however, which
is that you are sticking the loop form in as an extra, theoretically
"unused" argument to format. I think it is important for code clarity
that the format string make an honest attempt to consume the other
arguments (although format strings can of course include various skips
and jumps around the argument list), and not simply rely on them for
side-effects.

The result of your code is that the loop is executed *before* the outer
format completes (because Lisp has to evaluate the arguments to
functions before calling the function itself.

That's a *very confusing* way of organizing code that is generating
output. The "'" appearing FIRST in the format actually gets output
LAST, AFTER the loop has done all its format calls.

Perhaps you got to this point by accidental editing, but I find it much
better to use progn to ensure this kind of order.

E.g.

(progn (loop for cons on P
                    do (format t "~{~^ ~^'~a~^',~}" (car cons))
                    when (cdr cons) do (format t ","))
           (format t "'"))
From: markandeya
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1128690925.799617.228890@g49g2000cwa.googlegroups.com>
Dear Lisper Friends,
	 I made it with your help. emacs gave me trouble as it wouldn't save a
format nil statement to a variable with setq that it would print to the
editor screen. Really! I tried many times and ways, but today after
everyone's helpful ideas and code i got it all to work. i think it is
your collective energy and goodwill.
	 	Here is the simple working code that creates a quasi
multi-dimentional array in Java Script and which you can call with
myarray[4][2] syntax. The [4] represents the row and the [2] is the
field of the sql query result - of course arrays start at 0 not 1 so it
is row 5, field 3.
Here's the P result list from the sql statement again:
  ((Sarojini Grace 1) (Hari Om 2) (Mira Shanti 3))

	(setq mylist
  	     (format nil "~{[~{'~a'~^, ~}]~^, ~}" P))
		((:script "LANGUAGE"  "JavaScript")
		  (:princ
		   (format nil "<!-- Hide
		     numArray = new Array(~a);
		     document.write(\"[0]=\",numArray[0],\"<br>\");
		     document.write(\"[1]=\",numArray[1],\"<br>\");
		     document.write(\"[4]=\",numArray[2],\"<br>\");
		     document.write(\"[4][2]=\",numArray[2][2],\"<br>\");
		   //End Hide-->"
		     mylist)))
thanks again for all the help and i hope it is nice to see that your
help was fruitful. i learned alot and some of your replies are still
beyond me and i will come back to them. Markandeya
From: Geoffrey Summerhayes
Subject: Re: sending a format result to a string variable
Date: 
Message-ID: <1tJ1f.9252$2F2.1215925@news20.bellglobal.com>
"markandeya" <·········@sriaurobindoashram.com> wrote in message ·····························@g49g2000cwa.googlegroups.com...
>
> (setq mylist
>       (format nil "~{[~{'~a'~^, ~}]~^, ~}" P))
> ((:script "LANGUAGE"  "JavaScript")
>   (:princ
>    (format nil "<!-- Hide
>      numArray = new Array(~a);
>      document.write(\"[0]=\",numArray[0],\"<br>\");
>      document.write(\"[1]=\",numArray[1],\"<br>\");
>      document.write(\"[4]=\",numArray[2],\"<br>\");
>      document.write(\"[4][2]=\",numArray[2][2],\"<br>\");
>    //End Hide-->"
>      mylist)))
> thanks again for all the help and i hope it is nice to see that your
> help was fruitful. i learned alot and some of your replies are still
> beyond me and i will come back to them. Markandeya

One parting thought...

1>(let ((array '((Sarojini Grace 1) (Hari Om 2) (Mira Shanti 3)))
        (indices '((0)(1)(4)(4 2))))
    (format t
"~%<!-- Hide~%~
numArray = new Array(~{[~{'~a'~^, ~}]~^, ~});~%~
~{document.write(\"~{[~A]~}=\",numArray~:*~{[~A]~},\"<br>\");~%~}~
//End Hide-->~%"
          array indices))


<!-- Hide
numArray = new Array(['SAROJINI', 'GRACE', '1'], ['HARI', 'OM', '2'], ['MIRA', 'SHANTI', '3']);
document.write("[0]=",numArray[0],"<br>");
document.write("[1]=",numArray[1],"<br>");
document.write("[4]=",numArray[4],"<br>");
document.write("[4][2]=",numArray[4][2],"<br>");
//End Hide-->
NIL

--
Geoff