popping thngs

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

popping thngs

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

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

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

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

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

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

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

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

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

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

Locked