LISTs will be the death of me! Part 2

CheeseMyBaby
25 Apr 2018, 16:26

I'm printing a list using this:

foreach (x, game.pondered) {
  msg (x)
}

this prints the list like this:

ListItem1
ListItem2
ListItem3

(etc)

What I want to do

However, I want it to print the list like this:
ListItem1, ListItem2, ListItem3

What I really would like:

It would be slightly awesome if the list could print like so:
Things pondered: ListItem1, ListItem2 and ListItem3.

With a string at the start (Things pondered:), with commas in between the listitems and with an "and" before the last listitem.

Like always I'd very much appreciate all the help I can get!


K.V.
25 Apr 2018, 17:14
DisplayList (game.pondered, true)
DisplayList (game.pondered, false)

This is what you really want:

s = FormatList(game.pondered,",","and","nothing")
msg ("Things pondered: " + s + ".")

image


http://docs.textadventures.co.uk/quest/functions/string/formatlist.html

http://docs.textadventures.co.uk/quest/functions/#list

http://docs.textadventures.co.uk/quest/using_lists.html


CheeseMyBaby
25 Apr 2018, 17:20

S-a-weet...!

Thanks K.V.!!!


K.V.
25 Apr 2018, 17:22

No problem!

(I just edited the post. I forgot to add the helpful links!)


mrangel
25 Apr 2018, 17:33

You could write your own function to do this (and maybe trying to do it would help you to think in ways that could prove useful in future), but thankfully there are already functions that do the job in this case:

  • ListItem1, ListItem2, ListItem3 ← To do this, you can use msg (Join(game.pondered, ", ")). Join is the opposite of Split, it joins a list together using the string you specify.
  • Things pondered: ListItem1, ListItem2 and ListItem3. ← As KV says, there's a function called FormatList that does this.

If you think it would be interesting to work out how to do this yourself, here's one way you could do it:

  • output = "" // start with an empty string
  • foreach (x, game.pondered) { // loop over the elements of the list
    • if (not output = "") { // If we've already displayed some items…
      • output = outpur + ", " // …then add a comma before the next one
    • }
    • output = output + x // add the next element to the list
  • }
  • msg (output) // and display the result

The code above does the same as Join(), except that it will work with any list (not just a stringlist).


mrangel
25 Apr 2018, 17:40

(And thanks for the shout out KV :p Always nice to see myself mentioned. If you actually want to know what my next book will be about, I recently shared a brief summary and some prototype cover designs on facebook to seek feedback)


CheeseMyBaby
25 Apr 2018, 20:35

And thank you mrangel! I'm still trying to wrap my head around lists. Your explanation really helped me in doing so!


Dcoder
26 Apr 2018, 13:04

Deep Thoughts Side Note: Perhaps 8 buns to a pack is a conspiracy by the bun industry to force you to buy 16 buns instead of 8 (to cover 10 hot dogs), thus netting the bun industry an additional 60% profit, theoretically!

Or maybe the hot dog industry is just being generous in case a couple of dogs fall out of your bun -- i.e., you get 2 spares. I favor the former explanation, however.

Oh yeah, we were talking about lists...


CheeseMyBaby
26 Apr 2018, 16:21

Dcoder! That's super interesting! I might steal that thought for act III :)