Making a list with 'and' or 'or' before the final item

What is the best way to print a dynamic list with ‘and’ or ‘or’ before the final item? I can make a list with a dynamic string or just appending text conditionally no problem, but can’t figure out how to detect the final item and print ‘and’ before it (without a lot of laborious checking of flags to see what is still in the list and printing ‘and [item]’ rather than [item] if it happens to be the last thing):

apple, pear, banana and orange

(remove orange)

apple, pear and banana.

Will the built in list or set function do it (somehow) automatically? I know those are there, but they are undocumented in the manual. If it comes to it, I can live with an apple, pear, banana, orange list. But it niggles.

Just manually load up a collection, then use print list verbose to format it.

start_at = my_location

locations {
   my_location      : location "You are in a room." ;
}

collections {
   list_object_buffer : list;
}

strings {
   myvar : string;
}

on_command {
   
   : match "test _"  {
      : collection_clear "list_object_buffer";
      : set_string var = "myvar"  text = "" ;
      
      : collection_push { collection = "list_object_buffer" text = "a banana" }
      : collection_push { collection = "list_object_buffer" text = "an orange" }
      : collection_push { collection = "list_object_buffer" text = "a pear" }
      
      : print_list_verbose  "list_object_buffer"
         lead_in       = "You have "
         final_part    = "."
         final_sep     = " !!AND!! " 
         append_to_var = "myvar"
      ;

      : print {(myvar)}
   }

}

I created this page for documenting things that are not ready to go into the main documentation yet (subject to change):

https://adventuron.io/documentation/advanced.html

Currently it only documents the collection api, but that api may be interesting to you anyway.

1 Like

Exactly what was needed, and easy (if you know how…)

Thanks Chris. I’ll let you get on with your life again, for a few minutes.