Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is there a way to tell how many entries there are in a datamap in Harlowe?

I have some datamaps set up where I want to know how many entries there are in the datamaps. Is there an easy way to tell how many entries there are? I have considered adding an index number (like "Index", 1) to each entry and then check the number of the last entry, but that is not very convenient if I need to add or remove things from the datamaps and then have to renumber everything. Anyone have any ideas?

Comments

  • The javascript size property will return the number of entries.
    (print: $map.size)
    
  • Try the following:
    (set: $var to (datamap: "name", "John Doe", "age", 100, "hp", 0, "mp", 0))
    
    size: (print: $var.size), should be 4
    
  • edited September 2015
    That wasn't exactly what I was looking for, but it did put me on the right track to find $var.length which was exactly what I needed, because I was working with a 2 dimensional datamap and I wanted to find out how many rows it had.

    Thanks.
  • GerryS wrote: »
    That wasn't exactly what I was looking for, but it did put me on the right track to find $var.length which was exactly what I needed, because I was working with a 2 dimensional datamap and I wanted to find out how many rows it had.
    I am guessing that your outer container is not a (datamap:) because they don't have a length property, as shown by the following example which will cause an error when trying to access the length property of a (datamap:) $variable.
    (set: $var to (datamap: "name", "John Doe", "age", 100, "hp", 0, "mp", 0))
    
    length: (print: $var.length), will display "The (print:) macro needs 1 more value." error.
    
  • edited September 2015
    Well, I set it up the way you showed me a while back. I don't know if I'm doing it right or not, but it's working.
    (set: $LocationDM to (a:))
    (set: $dummy to $LocationDM.push((datamap: "Ocean", "Atlantic", "Town", "Innsmouth", "Country", "United Isles Of America")))
    (set: $dummy to $LocationDM.push((datamap: "Ocean", "Atlantic", "Town", "None", "Country", "United Isles Of America")))
    (set: $dummy to $LocationDM.push((datamap: "Ocean", "Atlantic", "Town", "None", "Country", "International Water")))
    (set: $dummy to $LocationDM.push((datamap: "Ocean", "Atlantic", "Town", "None", "Country", "International Water")))
    

    When I use $LocationDM.length it DOES return the value I'm looking for. Actually when I used $var.size like you said it returned "The (print:) macro needs 1 more value." error.
  • That is because the $LocationDM variable in your example is an Array, and that container type does have a size property. Your original question asked how to find the number of entries (key/value pairs) there are in a datamap, and that container type has a length property instead.

    I guess this is an instance where including an example of your story's code would of helped to clarify the question being asked.
Sign In or Register to comment.