Problem Concatenating Strings Extracted From a Table

I am trying to extract strings that are stored in a table and concatenate all of them into a single string, with each of the original strings from the table separated by commas. The code that I have developed to do this (see below) works fine in the Corona simulator for Windows, but produces undesired results on the iOS simulator (and when I test it on an actual iPad). Below is my code that illustrates the problem more specifically:

local stringTable = { "hello", "what", "then", "how" } local concatString = table.concat( stringTable, ", " ) print( concatString )

output on Windows simulator:

hello, what, then, how

output on iOS simulator:

hello,

what,

then,

how

As shown above, the string prints output all on one line in the Windows simulator (as expected), whereas it creates a new line to separate each of the original strings on the iOS simulator. I am displaying this string to the user using display.newText(), so I need the string to look like the output from Windows simulator on iOS.

I have tested the code above on multiple builds, including 2016.2686, 2016.2873, and 2016.2874, so I do not think it is a problem with the build I am using. I am pretty sure that this problem is a bug on Corona’s end, but would be very glad to be proved wrong or offered a work-around solution :slight_smile:

Oh, I should also mention that I have already tried using the code below as an alternative to using the table.concat() function, but it produces the same results as the code I provided in my original post.

local stringTable = { "hello", "what", "then", "how" } local concatString = "" for i = 1, #stringTable do if i == 1 then concatString = concatString..stringTable[i] else concatString = concatString..", "..stringTable[i] end end print( concatString )

output on iOS simulator:

hello,

what,

then,

how

When you say output, do you mean what’s displayed on the device or in the console?  Both codes work fine for me in the simulator (except in your first example need to change hintTable to stringTable).

Hi JonPM, Thanks for the response! Also, thanks for letting me know about the minor bug in my code (I went ahead and corrected it). When I say output, I guess I really mean both what’s displayed on the Corona simulator console (only the developer can see it), and what is displayed on the screen to the user (in the simulator or on a device). I need both of these to be the same. Below is the code that I am using to display the concatenated string on the screen as text:

local stringTable = { "hello", "what", "then", "how" } local concatString = table.concat( stringTable, ", " ) concatStringText = display.newText( { text = "", width = display.actualContentWidth - 80, height = 30, font = native.systemFontBold, fontSize = 9 } ) concatStringText.text = concatString
  1. On the screen of an iOS device (and as output in the iOS Corona console), this text looks like this:

hello,

what,

then,

how

 

  1. When I need it to look like this (all on one line):

hello, what, then, how

 

When you say the code I provided works fine for you, do you mean the output (both on the Corona simulator console and on the screen or device) look like number 1 or 2 above?

Don’t get hung up on the print statement. It’s possible it’s just just trying to wrap it for display purposes. I’ve seen it before. Your users will never see print statements. What happens if you create a display.newText() object and output your text that way?

Hi Rob - Thanks for the reply. When I output my concatenated string using the display.newText() object, I’m still having the same problem. The code that I am using for the display.newText() object is shown in my previous post. Again, this is only a problem on the Corona iOS simulator and when testing on an iOS device, and is not a problem when I test my code on the Corona Windows simulator.

Can you upload a small sample project that exhibits the same behavior?  Perhaps it’s something else that we’re not seeing.

Hi JonPM - Following your suggestion, I attempted to create a sample project that exhibited the behavior (see the attached documents). After testing this sample project on iOS Corona simulator, it appears that I am no longer having the issue. This sample project uses the exact same build.settings and config.lua files as I am using in my project, so the issue does not appear to have to do with these files. My project is pretty big now, so it looks like I may to have to do a lot troubleshooting to get to the bottom of this issue, but I’ll post back when I have made some progress on it.

Perhaps you are changing the text display objects width somewhere else in your code?

What version of Corona SDK are you using?

Rob and JonPM - Sorry for the delayed response - I have been out of town. To answer Rob’s question, this issue appears when using builds 2016.2686, 2016.2873, and 2016.2874. However, I have identified the problem and was able to fix it pretty easily. The problem did not have to do with the width of the displayed text, but thanks JonPM for suggesting that possibility.

More specifically, the the string elements I inserted into the table to begin with each had a new line following them. This was because all of the elements in the table I originally extracted from a text file using the following code. Sorry, I realize it probably would have been helpful to you if I mentioned this in my original post, but I did not think this would be the source of the problem.

readWordsFromFile = function( wordList ) local wordTable = {} local path = system.pathForFile( wordList, system.ResourceDirectory ) local file = io.open( path, "r" ) for line in file:lines() do line = string.sub( line, 1, #line ) wordTable[line] = true table.insert( wordTable, line ) end io.close( file ) file = nil return wordTable end local stringTable = readWordsFromFile( "stringList.txt" ) local concatString = table.concat( stringTable, ", " ) print( concatString )

I changed line 7 above to the following, and now the issue does not appear on neither the iOS simulator nor the Windows simulator:

line = string.sub( line, 1, #line - 1 )

I feel a little silly because Rob had actually suggested the code I posted in my previous post when responding to one of my posts last summer, which pertained to searching for elements in tables. The link to that thread is below:

https://forums.coronalabs.com/topic/56720-problem-searching-for-elements-in-a-very-large-table/

Sorry Rob for not catching this - I did not mean to waste your time on something that you had already addressed previously. Nonetheless, it is still odd that my original code worked perfectly fine on the Windows simulator, but not on iOS.

Thanks again Rob and JonPM for your assistance in solving this problem!

Oh, I should also mention that I have already tried using the code below as an alternative to using the table.concat() function, but it produces the same results as the code I provided in my original post.

local stringTable = { "hello", "what", "then", "how" } local concatString = "" for i = 1, #stringTable do if i == 1 then concatString = concatString..stringTable[i] else concatString = concatString..", "..stringTable[i] end end print( concatString )

output on iOS simulator:

hello,

what,

then,

how

When you say output, do you mean what’s displayed on the device or in the console?  Both codes work fine for me in the simulator (except in your first example need to change hintTable to stringTable).

Hi JonPM, Thanks for the response! Also, thanks for letting me know about the minor bug in my code (I went ahead and corrected it). When I say output, I guess I really mean both what’s displayed on the Corona simulator console (only the developer can see it), and what is displayed on the screen to the user (in the simulator or on a device). I need both of these to be the same. Below is the code that I am using to display the concatenated string on the screen as text:

local stringTable = { "hello", "what", "then", "how" } local concatString = table.concat( stringTable, ", " ) concatStringText = display.newText( { text = "", width = display.actualContentWidth - 80, height = 30, font = native.systemFontBold, fontSize = 9 } ) concatStringText.text = concatString
  1. On the screen of an iOS device (and as output in the iOS Corona console), this text looks like this:

hello,

what,

then,

how

 

  1. When I need it to look like this (all on one line):

hello, what, then, how

 

When you say the code I provided works fine for you, do you mean the output (both on the Corona simulator console and on the screen or device) look like number 1 or 2 above?

Don’t get hung up on the print statement. It’s possible it’s just just trying to wrap it for display purposes. I’ve seen it before. Your users will never see print statements. What happens if you create a display.newText() object and output your text that way?

Hi Rob - Thanks for the reply. When I output my concatenated string using the display.newText() object, I’m still having the same problem. The code that I am using for the display.newText() object is shown in my previous post. Again, this is only a problem on the Corona iOS simulator and when testing on an iOS device, and is not a problem when I test my code on the Corona Windows simulator.

Can you upload a small sample project that exhibits the same behavior?  Perhaps it’s something else that we’re not seeing.

Hi JonPM - Following your suggestion, I attempted to create a sample project that exhibited the behavior (see the attached documents). After testing this sample project on iOS Corona simulator, it appears that I am no longer having the issue. This sample project uses the exact same build.settings and config.lua files as I am using in my project, so the issue does not appear to have to do with these files. My project is pretty big now, so it looks like I may to have to do a lot troubleshooting to get to the bottom of this issue, but I’ll post back when I have made some progress on it.

Perhaps you are changing the text display objects width somewhere else in your code?