popping thngs

Started by tom, August 09, 2009, 06:17:47 AM

Previous topic - Next topic

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?

cormullion

#1
Perhaps you can work through a list using map, and pop while you go:


(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... :)

tom

#2
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.



(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.

:-)

cormullion

#3
I think what you've got is fine. Perhaps slightly cleaner:


(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:


(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)

tom

#4
Those are great improvements and I'll see where I can use them.  thanks!  I may get this working yet.

tom

#5
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?

cormullion

#6
... 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...


(if-not (null? expression)
                   (set 'output {stuff to show})
                   (set 'output {}))


which I use for my pages...

tom

#7
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?