Is it possible to print a list without linebreak, separated by commas?

I want to print a list with specific npc only with their names, but a list of names, each with a new line, doesn't look well.


Is it an objectlist?
In this case, you probably want something like:

some_npc_list = whatever function creates your list
npc_names = ""
foreach (npc, some_npc_list) {
  if (npc_names = "") {
    npc_names = GetDisplayAlias(npc)
  }
  else {
    npc_names = npc_names + ", " + GetDisplayAlias(npc)
  }
}
msg (npc_names)

If it's a stringlist containing their names, then you can just do:

msg (Join (list_of_npcs, ", "))

Or a little simpler:

npc_names = ""
foreach (npc, some_npc_list) {
  npc_names = npc_names + GetDisplayAlias(npc) + ", "
}
npc_names = Left (npc_names, Length(npc_names)-2)
msg (npc_names)

I've read so much about 'For each' but still don't understand what this function do, also 'For'. So, I tried this here and got a error, sayin' "unknown function: Length", But it is a function, I know :-/


Sorry, I meant LengthOf, not Length.
The hazards of writing code in many different languages; different function names to do the same thing.


"foreach" just makes a script do the same thing once for each item in the list.

In this case, for every item in the list, it adds that item's name and a comma to the end of the string. This means that you get a string whose contents is something like "John, Bob, Charlie, Lemonhead, Veronica, "
There's an extra comma at the end there; so you use npc_names = Left (npc_names, LengthOf(npc_names)-2) to cut the last two characters (a comma and a space) off the end of npc_names.


K.V.

Playing with lists:

my_list = Split("one;two;three;four;five",";")
msg (my_list)
DisplayList(my_list,false)
DisplayList(my_list,true)
foreach (item, my_list) {
  msg (item)
}
joined_list = Join(my_list,", ")
msg (joined_list)
formatted_list_UK = FormatList(my_list,", "," and")
msg (formatted_list_UK)
formatted_list_US = FormatList(my_list,", ",", and")
msg (formatted_list_US)

List: one; two; three; four; five;

  • one
  • two
  • three
  • four
  • five
  1. one
  2. two
  3. three
  4. four
  5. five

one
two
three
four
five
one, two, three, four, five
one, two, three, four and five
one, two, three, four, and five


My bad. I completely forgot about FormatList.

If you just want a comma-separated list, you can use FormatList(list_of_npcs, ", ", ", ", "Nobody").
(The last parameter is what you want to display if the list is empty)


Thanks...

foreach (npc, some_npc_list) { [...] GetDisplayAlias(npc)[...]

What is npc?


As KV pointed out, you can use FormatList instead of doing a foreach loop

But I think it's useful to learn how foreach works anyway; there'll be places you need to use it in future.

In this case, npc is each element in the list, one at a time.

If some_list is a list that contains Alan, Bob, and Charlie, then

foreach (npc, some_list) {
  [...]
}

would be the same as:

npc = Alan
  [...]
npc = Bob
  [...]
npc = Charlie
  [...]

(where the [...] can be whatever script as you want)


K.V.

That's a local variable.

You can put anything you want.

myList = Split("Joe;Bill;Larry", ";")
foreach(butthead, myList){
 msg(butthead)
}

Ah, I should look for new posts before klickin' on post.
So, I'm sure this is helping me very much especially 'FormatList'.

At the moment I use the 'For each' for quest goals and the journal, but only because people helped me, but I can't see how it works. Not sure why... Brain Blocked?


my way of explaining foreach (using Objects and Object Lists in/for this example. Using Strings and String Lists are a bit different with some of their syntax, but we can help you get them cleared/fixed up easily):

create ("joe_object")
joe_object.run_laps_script_attribute => msg ("Joe runs laps")

create ("jim_object")
jim_object.run_laps_script_attribute => msg ("Jim runs laps")

create ("john_object")
john_object.run_laps_script_attribute => msg ("John runs laps")

create ("team_object")

team_object.team_objectlist_attribute = NewObjectList ()
list add (team_object.team_objectlist_attribute, joe_object)
list add (team_object.team_objectlist_attribute, jim_object)
list add (team_object.team_objectlist_attribute, john_object)

foreach (team_member_variable, team_object.team_objectlist_attribute) {
  do (team_member_variable, "run_laps_script_attribute")
  //
  // conceptually behind the scenes what the 'foreach' does / is-doing:
  //
  // it's doing its scripting 'for-EACH (EVERY/ALL)' items in the provided list (this is called 'iterating', it's iterating through the List's items and doing its scripting for each/all/every-one of them), see below example explaining it
  //
  // team_member_variable = joe_object
  // do (team_member_variable, "run_laps_script_attribute")
  // do (joe_object, "run_laps_script_attribute")
  // output: Joe runs laps
  //
  // team_member_variable = jim_object
  // do (team_member_variable, "run_laps_script_attribute")
  // do (jim_object, "run_laps_script_attribute")
  // output: Jim runs laps
  //
  // team_member_variable = john_object
  // do (team_member_variable, "run_laps_script_attribute")
  // do (john_object, "run_laps_script_attribute")
  // output: John runs laps
}

P.S.

here's my guide on using Lists/Dictionaries, but it may confuse you more, as I'm not very good at explaining stuff...

http://textadventures.co.uk/forum/samples/topic/5137/list-and-dictionary-extensive-guide-by-hk

ask if you need help with anything


All variations are working, but finally 'FormatList' fits my needs more. Just printed the object list with commas...


K.V.

FormatList() is just doing the foreach() work for us:

  <function name="FormatList" parameters="list, joiner, lastjoiner, nothing" type="string"><![CDATA[
    if (ListCount (list) = 0) {
      return (nothing)
    }
    else {
      result = ""
      count = 0
      listLength = ListCount(list)
      foreach (item, list) {
        if (TypeOf(item) = "object") {
          result = result + GetDisplayName(item)
        }
        else {
          result = result + item
        }
        count = count + 1
        if (count = listLength - 1) {
          if (not IsRegexMatch("^\\W", lastjoiner)) {
            result = result + " "
          }
          result = result + lastjoiner + " "
        }
        else if (count < listLength) {
          result = result + joiner + " "
        }
      }
      return (Trim (result))
    }
  ]]></function>

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums