Page 1 of 1
popping thngs
Posted: Sun Aug 09, 2009 1:17 pm
by tom
here's another one:
I'm popping elements of a list into table cells, but I need a better way of progressing through the list. I know there is one but I can't remember what it is. I want to use each popped element twice, but once I pop the element it's gone!
or not. any ideas?
Posted: Sun Aug 09, 2009 4:47 pm
by cormullion
Perhaps you can work through a list using
map, and
pop while you go:
Code: Select all
(set 'l '("a1" "b2" "c3" "d4"))
(map (fn (e) (println e) (println (reverse e)) (pop l)) l)
(println l)
which uses each element twice and then removes them.
Do you
have to pop them at all?
It's a bit weird, destroying a list while you're mapping it, but seems to work OK... :)
Posted: Mon Aug 10, 2009 4:01 am
by tom
I probably don't have to pop anything, but the better solution is evading me.
Shield your eyes, here is a bit of code. stu is the list.
Code: Select all
(dotimes (x 5)(if (empty? stu)(push "Nobody" stu))(print (append "<td>"(string(inc w 1)) ". [["(set 'blap (pop stu))"]][image:" blap".gif ]</td>")))
If you stare at that too long, insanity may result. You have been warned.
:-)
Posted: Mon Aug 10, 2009 9:06 pm
by cormullion
I think what you've got is fine. Perhaps slightly cleaner:
Code: Select all
(for (x 1 5)
(println
(if (empty? stu)
(format {<td>%d. [[Nobody]][image:Nobody.gif]</td>} x)
(format {<td>%d. [[%s]][image:%s.gif]</td>} x (first stu) (pop stu)))))
It might be slightly easier if you 'fixed' the list before outputting it, so that your subsequent list manipulations are simpler:
Code: Select all
(set 'stu (append '("a" "b") (dup "Nobody" 3 true)))
(map (fn (n) (println (format {<td>%d. [[%s]][image:%s.gif]</td>} (+ $idx 1) n n))) stu)
Posted: Tue Aug 11, 2009 2:48 pm
by tom
Those are great improvements and I'll see where I can use them. thanks! I may get this working yet.
Posted: Wed Aug 19, 2009 1:34 pm
by tom
Ok, I have my famous web app working, more or less, but I've still got questions to assail you guys with. a generated web page displays newlisp errors that are expected but harmless. I tried to use some error handling functions to keep it from displaying, but with no luck. the page tries to display parts of a list that doesn't exist yet; click the submit button and the error goes away. What can I do about that?
Posted: Wed Aug 19, 2009 3:52 pm
by cormullion
... show us the code :)
Or rather, distill the problem to the smallest possible, then show us!
On the face of it it sounds like you should test lists before displaying them...
Code: Select all
(if-not (null? expression)
(set 'output {stuff to show})
(set 'output {}))
which I use for my pages...
Posted: Fri Aug 21, 2009 4:38 pm
by tom
I should probably keep all of my blathering about this to one topic--I just noticed I've started four of them so far for just simple questions. Sorry about that. In an effort to consolidate, I'll revisit a question from one of those other topics here: Why won't my modified newlisp wiki work in Internet Explorer in Windows? It works in Firefox in Windows. I solved the problem once, and it worked, but now I've gone and forgotten what I did. It was something simple. Any ideas?