3

I am building an ordered struct

stMbr = [:];

Lots and lots of fields get added.

stMbr.Name = "";
stMbr.Address = "";
stMbr.City = "";
...

Eventually I hit the last field that is being added. After the ordered struct is built, I am going to need to process it

for (key in stMbr)  {
   ...
}

When I process the last key, I need to do it note that I hit the last key.

Is there a way to know what the last key is in an ordered struct?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • What you mean ordered structure? You mean the sorting order of the key values? – Sathish Chelladurai Jun 06 '19 at 15:29
  • Newer versions of ColdFusion has a data type called an order struct. It is like a struct, but they order of the keys is preserved. It is created by using `[:]` or more verbosely `StructNew("Ordered")`. – James A Mohler Jun 06 '19 at 15:37

1 Answers1

1

It turns out to not be that hard. I just had to use the keylist() member function

if (key == listlast(stMbr.keylist()))  {
  ...
}

Updated Answer

Rather than reprocessing the same list, just keep the last key

lastKey = listlast(stMbr.keylist());


for (key in stMbr) {
...

if (key == lastKey)  {
  ...
  }
}
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 3
    That's not very efficient, is it? What do you want do with the last element? When there's an ordered struct you might know which element is the last and process that one. – Bernhard Döbler Jun 05 '19 at 07:22
  • 1
    I am passing ordered structs into csv generator. The last one needs to have a row separator rather than a columns separator. Hence I need to know when I am at the last one. – James A Mohler Jun 05 '19 at 14:24
  • 1
    Yeah, but you could do that before the loop so it only executes once. **Edit** ... which ... you figured out already. Never mind. Carry on :-) – SOS Jun 05 '19 at 19:36