the number of items

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, LCMark

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

the number of items

Post by mwieder » Sat Aug 10, 2013 5:59 pm

Recent posts on the use-list got me to investigate the issue of "the number of items", where the problem scenario is

Code: Select all

  put the number of items of "1,2,3,4" -- returns 4
  put the number of items of "1,2,3,4," -- returns 4 with final comma
  put the number of items of "1,2,3,4, " -- returns 5 with trailing space
So after a bit of poking around I found that chunk.cpp contains a countitems function as follows:

Code: Select all

static int4 countitems(MCExecPoint &ep, const char *sptr, const char *eptr)
{
	int4 items = 1;
	if (sptr < eptr)
		do
		{
			if (*sptr == ep.getitemdel() && sptr + 1 < eptr)
				items++;
		}
		while (++sptr < eptr);
	return items;
}
changing the conditional to

Code: Select all

			if (*sptr == ep.getitemdel())
fixes the problem of the final char being an itemDelimiter and returns the "proper" count in all cases. In addition, it solves the related problem of "the last char of" or "char -1 of" and returns empty or space where it should. The test for the end of the while loop correctly functions to terminate the test at the end of the string.

So... given that the function correctly does what's expected, I have two questions:

1. Why is the extra test in the conditional in the first place? It produces confusing, and IMO wrong, results and seems to provide no other purpose.
2. What would be the repercussions of making the change? Are there code scenarios that explicitly rely on the existing behavior? I can't come up with any off the top of my head, and in fact the only things that come to mind are scripts that might try to account for the current behavior by doing a secondary check and correcting for it. Database queries perhaps?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7252
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: the number of items

Post by FourthWorld » Sat Aug 10, 2013 6:19 pm

mwieder wrote:What would be the repercussions of making the change?
The wrath of Jacque. ;)
Are there code scenarios that explicitly rely on the existing behavior?
Given that xTalks have had this "feature" for 26 years, I'd be reluctant to change it. Not necessarily opposed, just reluctant. It would seem daunting to try to guess how much code in the community may be written to account for/rely on this.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sat Aug 10, 2013 6:30 pm

@Richard - yes, that's exactly why I'm bringing this up here for discussion. I don't have any code that relies on the current weird situation, and I don't have a notion of who might have code that does. I know Jacque posted on the list that she just expects a trailing comma to go with the preceding item, but I'm not sure what that means in terms of code that she might have in place.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sat Aug 10, 2013 6:34 pm

And I forgot to mention that there's another way out as well: a global session property that can be set from a script, something like

set the ignoreEmptyLastItem to false
where the default value is true for existing behavior

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 698
Joined: Fri Jun 27, 2008 9:00 pm

Re: the number of items

Post by Mikey » Sat Aug 10, 2013 7:13 pm

First of all, I wanted to thank Mark for being interested enough in this annoyance (to me) to dig into it.
Second, I love his idea for a way to deal with it.

I run into this frequently, not just with items, but with other chunks as well, especially lines. The worst is when I am dealing with databases, where at least for me, the queries I perform tend to have the most significant columns first and the least significant ones last. Frequently, the least significant columns are optional and may not hold any value. So unless I hard code the number of columns into REPEAT loops (instead of counting the number of items returned, and remember to change the REPEAT when I modify the query), when I get to an INSERT or an UPDATE, I have a good chance of getting an error from the database drivers because I'm not sending in as many values as I have columns specified in the query.

Yes, there are ways around that. They're all pains, they makes the code much harder to read, and this is one of the gotchas that I have to remind others about, because debugging on mobile is, as we all know, a wee bit lacking, and right now, we are writing for nothing but mobile.

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 698
Joined: Fri Jun 27, 2008 9:00 pm

Re: the number of items

Post by Mikey » Sat Aug 10, 2013 7:17 pm

and I see that my account doesn't seem to have my KS badge :-( Dang it, I paid a big pile of coin for that badge :-( HEATHER!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sat Aug 10, 2013 7:33 pm

@Mike- so it sounds like your current workarounds (repeating with the number of coumns rather than the number of items) wouldn't break if this change were made, i.e., you wouldn't have to do any backpedaling to fix existing scripts because current stacks were breaking.

... and email Heather about the badge. She'll get you sorted out. :-)

paul_gr
Posts: 320
Joined: Fri Dec 08, 2006 7:38 pm
Location: Blenheim, New Zealand

Re: the number of items

Post by paul_gr » Sat Aug 10, 2013 9:06 pm

I use "set the itemDelimiter to space" a lot
In numerical calculations I prefer separate the items with a space, not a comma.
This gives me a better visual flow.
Hopefully any changes suggested will not affect usage of setting the itemDelimiter to any separator we like.

just my 2c.

Paul

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sat Aug 10, 2013 9:28 pm

@Paul - that's an interesting one. The only thing that would be affected is a space, i.e.,

Code: Select all

set the itemDelimiter to space
put the number of items in "1 2 3 4" -- returns 4
put the number of items in "1 2 3 4 " -- returns 5
I think that's what I would expect: in the last statement item 5 is empty. Currently the last statement would return 4 because the final space is the last character in the string. The first statement would return 4 items in either case.

Any thoughts?

paul_gr
Posts: 320
Joined: Fri Dec 08, 2006 7:38 pm
Location: Blenheim, New Zealand

Re: the number of items

Post by paul_gr » Sat Aug 10, 2013 9:39 pm

@mweider.
Good point.
When using the space as a delimiter I just make sure trailing spaces are not there; they have caused me issues in the past.
As long as things are consistent I don't mind having to trim "unconstrained" spaces from the ends of a string.

Paul

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sat Aug 10, 2013 9:51 pm

they have caused me issues in the past.
LOL. Exactly.

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1561
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: the number of items

Post by monte » Sat Aug 10, 2013 10:26 pm

I believe this issue is seen with lines too:

Code: Select all

put the number of lines of " "&cr&cr -- returns 1
I know this has come up many times in the past and it's always backwards compatibility cited as the reason not to fix it. FWIW I don't think I have any code that relies on this quirk. Whenever building a list (something which is a bit rare these days... much prefer working with arrays) I always ensure there's no trailing delimiters with either:

Code: Select all

delete char -1 of tList
or adding the delimiters before the line/item.

I'd recommend ignoreLastDelimiter be a stack property rather than a global one so that stacks that use each can play happily next to each other. Perhaps existing stacks could have it default to true but newly created stacks would default to false?
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Re: the number of items

Post by malte » Sat Aug 10, 2013 10:39 pm

I always found it strange too... Now here is the but

Anybody of you remember the day when we noticed that the filter command was "fixed" not to remove the trailing CR when you did a filter without empty?

I certainly do, as this "tiny" change to the filter command forced me to go through an application containing 50+ stacks, at least 2 or 3 cards each, tons of script in summary and change an enormous amount of code.

I surely would not want to put anybody else (relying on the current behaviour) to go through a pain like this, so at least an option to switch back to the current behaviour is a must.

I would be happy with a stack property for this, but messing with the way chunks work is IMHO at least potentially very very dangerous.

3 cents due to inflation.

Malte

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7252
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: the number of items

Post by FourthWorld » Sat Aug 10, 2013 11:29 pm

malte wrote: always found it strange too... Now here is the but

Anybody of you remember the day when we noticed that the filter command was "fixed" not to remove the trailing CR when you did a filter without empty?

I certainly do, as this "tiny" change to the filter command forced me to go through an application containing 50+ stacks, at least 2 or 3 cards each, tons of script in summary and change an enormous amount of code.
Bingo.

We may not have code that relies on this anomaly from the HyperTalk team, but many probably have code that expects it, and is therefore dependent on it.

I'd hate to see LC become like Python, where its users have to decide between versions because of a lack of backward compatibility, where the newer version has more features but the older version has more libraries available for it.

Can we find any languages outside of xTalk that count delimited elements this way? I wonder if what's needed here is more of a good explanation for how it is than to simply toss it.

Anyone here know why the HyperTalk team implemented this the way they did?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2894
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: the number of items

Post by mwieder » Sun Aug 11, 2013 12:52 am

@Malte, Richard - Yes, that's why I'm bringing this up for discussion. Since it's so easy to fix, the reason it hasn't been done is one of superstition: there's possibly something out there that might break if we change the code, but nobody's actually seen one.
I'd like to take this out of the occult and see if anyone has any code that actually relies on the current behavior in a way that would break if it were fixed.
In other words, code that explictly needs there to be a difference in the number of items in a string with a trailing comma versus a trailing comma+space.

@Monte - The thing with making a list is the one case I thought of where this might break things. I also remove trailing commas, crs, whatever, but it's possible that anyone making a comma-separated list and then looping through the number of items would now end up with an empty last item. I'm not sure that counts as breaking, but a good case could be made for it.

Post Reply

Return to “Engine Contributors”