building html list from a newLISP list and then to nL list

Q&A's, tips, howto's
Locked
joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

building html list from a newLISP list and then to nL list

Post by joejoe »

i am trying to take a list of words and transform it into an html list.

the list:

Code: Select all

one
two
three
what I am trying to construct is this:

Code: Select all

<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
what i am trying is this:

Code: Select all

(set 'l (read-file "./list"))
(parse l)
(replace $it l "<li><a href=\"http://xyz.com/$it\">$it</a></li>")
(println l)
however, i am getting the error:

Code: Select all

ERR: string expected in function replace
the manual entry for the replace function says:

syntax: (replace exp-key list exp-replacement [func-compare])

i thought $it is the exp-key, l is the list, and the exp-replacement to be:

Code: Select all

"<li><a href=\"http://xyz.com/$it\">$it</a></li>"
am i correct to guess that replace is the correct function to do this?

thanks very much!
Last edited by joejoe on Sat Nov 14, 2009 11:27 pm, edited 1 time in total.

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: building html list from a newLISP list

Post by xytroxon »

Use format function, doing string %s replacement(s)...

Code: Select all

; (set 'input (read-file "./list"))
; Note: For my test, I will use this string below instead.
(set 'input "one\r\ntwo\tthree four") 

; format list items using %s (twice), push to output list
(dolist (item (parse input))
	(push (format "<li><a href=\"http://xyz.com/%s\">%s</a></li>" item item) output -1)
)

; display as list
(println output)
(println)

; display each string
(dolist ( item output)
	(println item)
)
(exit)

Code: Select all

("<li><a href=\"http://xyz.com/one\">one</a></li>" "<li><a href=\"http://xyz.com/two\">two</a></li>" 
 "<li><a href=\"http://xyz.com/three\">three</a></li>" "<li><a href=\"http://xyz.com/four\">four</a></li>")

<li><a href="http://xyz.com/one">one</a></li>
<li><a href="http://xyz.com/two">two</a></li>
<li><a href="http://xyz.com/three">three</a></li>
<li><a href="http://xyz.com/four">four</a></li>
-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: building html list from a newLISP list

Post by cormullion »

The system variables like $it are set as the result of some function running, so you use them 'after' rather than 'before'. "During executions of the replacement expression, the system variable $0 and the anaphoric system variable $it are set to the expression to be replaced."

I like the format approach, but you can stick with replace and $it:

Code: Select all

(set 'lst '("one" "two" "three"))
(dolist (i lst)
  (replace i lst (string "<li><a href=\"http://xyz.com/" $it "\">" $it "</a></li>")))
but it seems a bit clumsy to me.

How about:

Code: Select all

(set 'lst '("one" "two" "three"))
(map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list

Post by joejoe »

xytroxon wrote:Use format function, doing string %s replacement(s)...

Code: Select all

; format list items using %s (twice), push to output list
(dolist (item (parse input))
	(push (format "<li><a href=\"http://xyz.com/%s\">%s</a></li>" item item) output -1)
)
(exit)
-- xytroxon
!! awesome thanks xytroxon -- im really diggin the format function now. :0)

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list

Post by joejoe »

cormullion wrote:The system variables like $it are set as the result of some function running, so you use them 'after' rather than 'before'. "During executions of the replacement expression, the system variable $0 and the anaphoric system variable $it are set to the expression to be replaced."

I like the format approach, but you can stick with replace and $it:

Code: Select all

(set 'lst '("one" "two" "three"))
(dolist (i lst)
  (replace i lst (string "<li><a href=\"http://xyz.com/" $it "\">" $it "</a></li>")))
but it seems a bit clumsy to me.

How about:

Code: Select all

(set 'lst '("one" "two" "three"))
(map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)
thanks supremely cormullion!

the dolist now makes full sense to me and the map and fn are coming clear! :-)

thanks for the rapid refire which provided two educational examples.

i appreciate the help from both you cormullion, and xytronox on this!

i feel like im being assisted by two benign space invaders. :D

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list

Post by joejoe »

to make use of the above methods, im at a wall.

once something has been changed with map, i cant seem to get the new list out of it to do something with.

i tried:

Code: Select all

(set 'lst '("one" "two" "three"))
(set 'list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))
(println list)
(exit)
but it gives:

Code: Select all

ERR: symbol is protected in function set : list
i also tried set-ref-all and list and define to get the new list back out:

i replaced the second line above with both of these:

Code: Select all

(set-ref-all newlst (list list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)))
and

Code: Select all

(define list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))
am i anywhere close on any of these? thanks kindly again.

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: building html list from a newLISP list

Post by itistoday »

joejoe wrote:but it gives:

Code: Select all

ERR: symbol is protected in function set : list
'list' is a built-in function. You can't say (set 'list something), just pick a different symbol name.
Get your Objective newLISP groove on.

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list

Post by joejoe »

itistoday wrote:
joejoe wrote:but it gives:

Code: Select all

ERR: symbol is protected in function set : list
'list' is a built-in function. You can't say (set 'list something), just pick a different symbol name.
rodger that --

wont ever make that mistake again!

thanks for the reply, itistoday.

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list and then to nL list

Post by joejoe »

hmm... still doesnt seem to get the new list out with all the pieces.

this:

Code: Select all

(set 'lst '("one" "two" "three"))
(set 'hlist (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))
(println hlist)
replies:

Code: Select all

<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
("</a></li>" "</a></li>" "</a></li>")
which i think means that hlist is the last line above.

i know im closer than ever.

does the map function output directly into the set as tried here? it seems to set the list variables each as

Code: Select all

"</a></li>"

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: building html list from a newLISP list and then to nL list

Post by cormullion »

This is something that's easy to trip up over if you're not fully converted to functional programming... :) Every function returns a value. println returns a value. println also has a useful side-effect - it prints stuff. But it returns a value too.

In the newLISP console, type this:

Code: Select all

>(set 'item "one")
"one"
> (println {<li><a href="/} item {">} item {</a></li>})
<li><a href="/one">one</a></li>
"</a></li>"
You'll see that, after the printing, the value returned by the println expression is "</a></li>" - the last value produced. So although the output was what you wanted, the return value wasn't. And it's that return value that is collected up and stored by map that you've stored in the list. Hence the collection of strings you've got in your hlist.

If you want the output to be printed and the same output to be returned as a value, you have to take steps to output and return the same string:

Code: Select all

(set 'hlist (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))
The string function returns a value, which is printed by println and returned by println as the result of the anonymous function. So your result will be the strings in a list:

Code: Select all

("<li><a href=\"/one\">one</a></li>" "<li><a href=\"/two\">two</a></li>" "<li><a href=\"/three\">three</a></li>")

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: building html list from a newLISP list and then to nL list

Post by Lutz »

… or, when you are really not that interested in the return value, perhaps a 'dolist' is more appropriate:

Code: Select all

(dolist (item lst) (println {<li><a href="/} item {">} item {</a></li>}) )

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list and then to nL list

Post by joejoe »

i apologize up front for not getting the concept.

i still cant get the html to be set to a variable that i can then use.

i tried with the code Lutz put up:

Code: Select all

(set 'lst '("one" "two" "three"))
(set 'html (dolist (item list) (println {<li><a href="/} item {">} item {</a></li>}) ))
(println html)
but when i try to set it to a variable, as i added in above, it results in:

Code: Select all

ERR: list expected in function dolist : list
i also tried the example cormullion wrote:

Code: Select all

(set lst '("one" "two" "three"))
(set 'html (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))
(println html)
result here
:

Code: Select all

ERR: symbol expected in function set : lst
in case i misread Lutz's post, i also tried this with (i think) the correct list:

Code: Select all

(set 'lst '("one" "two" "three"))
(set 'html (dolist (item lst) (println {<li><a href="/} item {">} item {</a></li>}) ))
(println html)
which results in:
<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
</a></li>
and i think the fourth line is the 'html list, which hasnt been populated or something?

im struggling for sure. :]

gracious thanks again !

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list and then to nL list

Post by joejoe »

i got it!

my mistake on not putting the ' in front of the symbol when adding to cormullions code.

Code: Select all

(set 'lst '("one" "two" "three"))
(set 'html (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))
(println html)
results in this:

Code: Select all

<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
("<li><a href=\"/one\">one</a></li>" "<li><a href=\"/two\">two</a></li>" "<li><a href=\"/three\">three</a></li>")
woo hoo!!! :D

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: building html list from a newLISP list and then to nL list

Post by xytroxon »

Edited: LOL!!! Your 2nd post beat mine... Do you want a list of strings or one html string???

If you want one string, then:

Code: Select all

(set 'lst '("one" "two" "three"))

(set 'html "")

(dolist (item lst)
   (push (format "<li><a href=\"http://xyz.com/%s\">%s</a></li>\r\n" item item) html -1)
)

(println html)
(exit)
Note 1: html MUST be either a preexisting string value or set to the empty string "" when pushing strings to it, otherwise, html will become a list of strings!

Note 2: The format string ends with the Windows' newline \r\n, remove or change for your system's newline char(s).

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: building html list from a newLISP list and then to nL list

Post by joejoe »

xytroxon wrote:Edited: LOL!!! Your 2nd post beat mine... Do you want a list of strings or one html string???
in the end, one string that i can put into an existing html file, replacing a similar one.

right now, i appreciate them both because its teaching me lots! :0)
xytroxon wrote:If you want one string, then:

Code: Select all

(set 'lst '("one" "two" "three"))

(set 'html "")

(dolist (item lst)
   (push (format "<li><a href=\"http://xyz.com/%s\">%s</a></li>\r\n" item item) html -1)
)

(println html)
(exit)
thats awesome and simple (in the best way!). thanks for yet another way!!
xytroxon wrote:Note 1: html MUST be either a preexisting string value or set to the empty string "" when pushing strings to it, otherwise, html will become a list of strings!

Note 2: The format string ends with the Windows' newline \r\n, remove or change for your system's newline char(s).

-- xytroxon
gotcha - im starting to see things more clearly.

much appreciated and thanks to all for the help with this.

Locked