Read userInput & store in file & display on screen

this is it:

–  

local function readSnapData( event )  –  chg ( userInput ) to ( event )?

pathSnapAOpenRead, errorStringpathSnapAOpenRead = io.open( pathSnapA , “r” )  –  open for read

if not pathSnapAOpenRead then

–  Error; output cause

print( "File error: " … errorStringpathSnapAOpenRead )  –  DEBUGmrp

else

–  Grab lines & stick in to table

index = 0  –  start index at zero then increment inside loop to correct start value (1)

for line in pathSnapAOpenRead:lines() do

if line then

   index = index + 1  –  increment to get correct table index

   snapTable[“index”] = line

else

   print( "data not found in file pathSnapA  ", pathSnapA )

print( "capture new user inputs to populate snap-shot file   ", pathSnapA )

end

end

–  Close the file – move to end of processing; final tasks section

–  io.close( pathSnapAOpenRead )  – close file / handle

–   return true

end

–  put at end w/ final tasks    pathSnapAOpenRead = nil  –  release memory allocation for file handle

end

–  --------------------------------------------------------------------------

try this:

[lua]

–  

local function readSnapData( event )  –  chg ( userInput ) to ( event )?

pathSnapAOpenRead, errorStringpathSnapAOpenRead = io.open( pathSnapA , “r” )  –  open for read

if not pathSnapAOpenRead then

–  Error; output cause

print( "File error: " … errorStringpathSnapAOpenRead )  –  DEBUGmrp

else

–  Grab lines & stick in to table

index = 0  –  start index at zero then increment inside loop to correct start value (1)

for line in pathSnapAOpenRead:lines() do

if line then

   index = index + 1  –  increment to get correct table index

   snapTable[“index”] = line

else

   print( "data not found in file pathSnapA  ", pathSnapA )

print( "capture new user inputs to populate snap-shot file   ", pathSnapA )

end

end

–  Close the file – move to end of processing; final tasks section

–  io.close( pathSnapAOpenRead )  – close file / handle

–   return true

end

–  put at end w/ final tasks    pathSnapAOpenRead = nil  –  release memory allocation for file handle

end

–  --------------------------------------------------------------------------

[/lua]

continuing with this, from the sample code/storage/saveTable/main.lua file, in the function loadData(), it calls out a library str that is not found by the simulator.  it doesn’t look like a typo for string.  it is used to split the line up & reformat it.

my goal is to take a table, store in a file, retrieve it later and put into the original table it came from via an intermediate table;

Where is the ‘str’ library & how do I get it ready to use?

Thanks.


[lua]

local str = require(“str”)  –  LIBRARY CAN NOT BE LOCATED

if file then

– Read file contents into a string

local dataStr = file:read( “*a” )

– Break string into separate variables and construct new table from resulting data

local datavars = str.split(dataStr, “,”)

dataTableNew = {}

for i = 1, #datavars do

– split each name/value pair

local onevalue = str.split(datavars[i], “=”)

dataTableNew[onevalue[1]] = onevalue[2]

end

io.close( file ) – important!

– Note: all values arrive as strings; cast to numbers where numbers are expected

dataTableNew[“numValue”] = tonumber(dataTableNew[“numValue”])

dataTableNew[“randomValue”] = tonumber(dataTableNew[“randomValue”])

else

print (“no file found”)

end

[/lua]

Where did you get this code?  The only reference to str after the require is a “str.split()”. The standard Lua string library does not have a split function. The split function is an easy to break a long string into many short strings based on some divider. In the example above it’s dividing on a comma.

There are plenty of versions of a split function that can be found online. The one I use is this:

function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end

My version attaches it to the string library namespace, so instead of str.split, it would be string.split. Drop that into your code where you’re trying to require the str library and change the use of it later to string.split instead of str.split.

Rob

Rob

Thank you for responding.  I changed the require & the commands str to string.  There is still a problem.

here is the setup:

local string = require(“string”)

command (one example)

local datavars = string.split(dataStr, “,”)  –  ? will this work w/ fields having ‘,’ in them?

From OUTPUT window:

“F:\app00000006a\MPGcalcA\main.lua:201: attempt to call field ‘split’ (a nil value)\nstack traceback:\n F:\app00000006a\MPGcalcA\main.lua:201: in function <F:\app00000006a\MPGcalcA\main.lua:170>\n ?: in function <?:205>”

F:\app00000006a\MPGcalcA\main.lua:201: attempt to call field ‘split’ (a nil value)

stack traceback:

F:\app00000006a\MPGcalcA\main.lua:201: in function <F:\app00000006a\MPGcalcA\main.lua:170>

?: in function <?:205>

Debugging session completed (traced 0 instructions).

You don’t need to require(“string”). You just need to copy/paste that function I provided instead of doing:

str = require(“str”)

Rob

the code is directly from the sample code collection.

see C:\Program Files (x86)\Corona Labs\Corona SDK\Sample Code\Storage\SaveTable

What version of Corona SDK are you using?

I just checked on my Mac with 2929 and it’s there.

Here is the source code for it:

-- This adds string "split" functionality similar to "explode()" in PHP local M = {} M.split = function( str, pat ) local t = {} local fpat = "(.-)" .. pat local last\_end = 1 local s, e, cap = str:find( fpat, 1 ) while s do if ( s ~= 1 or cap ~= "" ) then table.insert( t, cap ) end last\_end = e + 1 s, e, cap = str:find( fpat, last\_end ) end if ( last\_end \<= #str ) then cap = str:sub( last\_end ) table.insert( t, cap ) end return t end return M

2016.2830  simulator version (“about similator”)

win 8.1 64bit

I copied code from the tablesave sample.

2830 is a couple of public builds old now. 2906 is the current public build and daily builds are up to 2932. You might want to consider updating your Corona SDK. I just checked on 2932 and str.lua is in the folder on Windows.

Rob

https://docs.coronalabs.com/daily/api/

1 - ‘str’ not shown in docs & no string.split() either.

2 - are you sure this is part of the build or a customization?

3 - note that the sample file savetable was delivered w/ the version I have running

   showing the “str.split()” fcn.

I’m going to update my version anyway.  Not sure if daily or public yet.

Thank you much for the information, etc.

Str and string.split are not build in APIs.

For string.split see
https://coronalabs.com/blog/2013/04/16/lua-string-magic/

“str” is not a standard API library. string.* is. However the a bunch of other “string manipulation” functions that are not part of the Lua standard string.* library that lots of people like to use. As a developer you would create a lua file in your project and in that file you can add your own library of functions, even adding them to the string.* name space for you app. string.split (or as the savetable sample uses it:  str.split more on that in a bit).

If you want a split function you have to add it yourself. That means either writing your own or finding on on the internet or having a developer help you out. You can include it in your code where you need it, or you can make your own “string” module … just don’t name it “string”. This is what the sample app you’re looking at does.

It has a file called str.lua in the folder. It simply has a string.split function in it and in the way that sample app is working you end up calling “str.split()”. In my case I have an “extras.lua” or “utility.lua” file that I put random useful functions in. But I use an appropriate name space for several of the functions. For instance I don’t call “utility.split()”, I have it so I add the split function to the string namepsace and do string.split().

Rob