# syntax for number of elements in table does not work on table with elements present

The table snapDataToWrite{} in defined outside the function (it contains values entered by user).

When the “done” button is pressed, this function should run & write these table values into a file for

permanent storage.

The loop index, using the nomenclature described, #snapDataToWrite, is ZERO.  Not 20 matching the

number of elements shown in the print() of the table.

Why is this happening?  I don’t see any info at all on this “#” notation except one para on a getting started page that shows this.  I have seen it in some examples, too.  The table is populated from listeners (“tap”).

Let me know if you are aware of what is happening here or if something is not right in the code.  Thanks much.

[lua]

local function buttonSnapDoneDo( event )

print( "buttonSnapDoneDo being executed . . .  ")

    print( "snapDataToWrite  =  ", snapDataToWrite )

    print( "#snapDataToWrite  =  ", #snapDataToWrite )

–   (etc)

[\lua]

"snapDataToWrite  =  " {userAddress1 = “e”, userAddress2 = “f”, userAutoMake = “m”, userAutoModel = “n”, userAutoTag = “o”, userCell = “k”, userCity = “g”, userCust1 = “s”, userCust2 = “t”, userEmail = “l”, userID1 = “p”, userID2 = “q”, userID3 = “r”, userName1 = “a”, userName2 = “b”, userName3 = “c”, userName4 = “f”, userPhone = “j”, userState = “h”, userZip = “i”}

"#snapDataToWrite  =  " 0

The # only works on strings and numerically indexed arrays. Tables indexed by keys (i.e. hash tables, associative arrays), cannot use # to get a count.

To count a key indexed table, you would need:

local n = 0 for k, v in pairs( tableName) do     n = n + 1 end

Thanks for that info.  I recall seeing something like that now.  I am making progress.  Much appreciated.

I don’t see any info at all on this “#” notation except one para on a getting started page that shows this.

Since corona projects are written in lua which is a widely used and fairly well documented language, if there’s anything you need to find that isn’t listed here, try google for general lua information. Someone somewhere has probably asked the same question for whatever you might need.

http://www.lua.org/manual/5.1/manual.html#2.5.5

OK.  Good point!  Thanks a bunch.  Much appreciated.

It’s also worth mentioning, that even with numerically indexed arrays if you have ‘gaps’ in your array the # operator will only go as high as the first gap.

E.g. if you have this table:

local myTable = { [1] = "hello", [2] = "world", [3] = "foo", [5] = "bar", [6] = "hello again", }

And you try to find out how many entries there are using # as follows:

print(#myTable)

The result would be 3, because 3 is the last index it reaches before it hits a gap (because there is no entry at [4]). The same would be true if I had put [4] = nil.

It’s an easy mistake to make at first because you may expect it to return 5 because there are 5 entries, or 6 because that is the highest index. If you do need to get the highest entry, you can always use 

table.maxn(myTable)

The # only works on strings and numerically indexed arrays. Tables indexed by keys (i.e. hash tables, associative arrays), cannot use # to get a count.

To count a key indexed table, you would need:

local n = 0 for k, v in pairs( tableName) do     n = n + 1 end

Thanks for that info.  I recall seeing something like that now.  I am making progress.  Much appreciated.

I don’t see any info at all on this “#” notation except one para on a getting started page that shows this.

Since corona projects are written in lua which is a widely used and fairly well documented language, if there’s anything you need to find that isn’t listed here, try google for general lua information. Someone somewhere has probably asked the same question for whatever you might need.

http://www.lua.org/manual/5.1/manual.html#2.5.5

OK.  Good point!  Thanks a bunch.  Much appreciated.

It’s also worth mentioning, that even with numerically indexed arrays if you have ‘gaps’ in your array the # operator will only go as high as the first gap.

E.g. if you have this table:

local myTable = { [1] = "hello", [2] = "world", [3] = "foo", [5] = "bar", [6] = "hello again", }

And you try to find out how many entries there are using # as follows:

print(#myTable)

The result would be 3, because 3 is the last index it reaches before it hits a gap (because there is no entry at [4]). The same would be true if I had put [4] = nil.

It’s an easy mistake to make at first because you may expect it to return 5 because there are 5 entries, or 6 because that is the highest index. If you do need to get the highest entry, you can always use 

table.maxn(myTable)