Author Topic: Mappings and arrays/lists.  (Read 3060 times)

Offline Konji

  • Acquaintance
  • *
  • Posts: 28
    • View Profile
Mappings and arrays/lists.
« on: August 04, 2010, 01:25:28 PM »
I just wanted to clarify some things....

([ ]) denotes a mapping, NOT [ ] (there must be parens?)
({ }) denotes an array (Must be parens?)

So then, you can have:
   ([
      ({"this", "that", "and that one other thing"}): "Now I remember!"
   ])
BUT NOT
  [
    {"1", "2", "3"}: "4"
  ]

EDIT: Also, in SetInventory, you can: ([ "path/to/orc": "growl" ]). Can you specify multiple orcs that growl on one line? ([ "path/to/orc": ({2, "growl"}) ])
« Last Edit: August 04, 2010, 01:28:20 PM by Konji »

Offline chaos

  • BFF
  • ***
  • Posts: 291
  • Job, school, social life, sleep. Pick 2.5.
    • View Profile
    • Lost Souls
Re: Mappings and arrays/lists.
« Reply #1 on: August 04, 2010, 03:38:01 PM »
Regarding parens, yes, in both cases the parentheses are part of the syntactic token, and you cannot use bare brackets or braces as mapping or array constructors.

Your second question is a mudlib-specific question, not an LPC question, and should probably be posted to the support or discussion area for your lib.

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1020
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: Mappings and arrays/lists.
« Reply #2 on: August 04, 2010, 05:38:16 PM »
Code: [Select]
  ([
      ({"this", "that", "and that one other thing"}): "Now I remember!"
   ])

Minor comment/nitpick...Using an array as a mapping key isn't likely to work well in MudOS/FluffOS
if the idea is to use that key to refer to the value. This syntax is legal, but probably not useful for that.

You can probably find this in a lib, for example in situations where the mud is trying to match arbitrary
input with one thing, in which case you enter a loop and go through all the elements in the map.

But for something like this:
Code: [Select]
MapName[({"this","that"})]
I don't believe I've seen that work in MudOS/FluffOS. Maybe some other driver does it.

-Crat

Offline chaos

  • BFF
  • ***
  • Posts: 291
  • Job, school, social life, sleep. Pick 2.5.
    • View Profile
    • Lost Souls
Re: Mappings and arrays/lists.
« Reply #3 on: August 04, 2010, 05:42:21 PM »
Yeah, it's impossible for
Code: [Select]
MapName[({"this","that"})] (that is, a retrieval based on a constructor-specified array)

to work in any LPC variant I know anything about, because arrays (and mappings) are pointers.  So in order to retrieve using an array key, you have to provide the same array as was used as a key when the value was assigned, not a different array with the same contents.  This doesn't make array or mapping keys completely useless, just mostly.

Offline chaos

  • BFF
  • ***
  • Posts: 291
  • Job, school, social life, sleep. Pick 2.5.
    • View Profile
    • Lost Souls
Re: Mappings and arrays/lists.
« Reply #4 on: August 04, 2010, 05:44:27 PM »
Simple illustration of the principle at play:

Code: [Select]
eval return ({ "a" }) == ({ "a" });
and

Code: [Select]
eval string array a = ({ "a" }); return a == a;

Offline Konji

  • Acquaintance
  • *
  • Posts: 28
    • View Profile
Re: Mappings and arrays/lists.
« Reply #5 on: August 04, 2010, 10:38:57 PM »
Ok, thanks guys. Yeah, I'm using Dead Souls mudlib. I'll post over there about the second quesiton.

Offline quixadhal

  • BFF
  • ***
  • Posts: 642
    • View Profile
    • WileyMUD
Re: Mappings and arrays/lists.
« Reply #6 on: August 06, 2010, 10:35:53 AM »
Would be kinda neat though.  It would give you a many-to-one mapping, which you really can't do right now.  I believe it would also make mappings 100% reversable.

Right now, you can reverse a mapping (swap the keys and values), but if you have same value assigned to multiple keys, it's undefined which one will end up being the new key, and thus which old key ends up being its value.

In this case, I'm thinking of the mapping data type being able to use the contents of an array key, rather than just the pointer.  If it understood that, it could fold multiple keys into the same place, and thus a reversal would work as you might expect it to.

I suspect the only places this would really find any benefit would be in database work (M:1 maps), and perhaps in parsers to be able to have a whole set of synonyms point to the same coderef or other data block.