2

我不知道 sqlite,但我必须实现一个已经完成的数据库。我正在使用 Corona SDK 进行编程。问题:我有一个名为“答案”的列,格式为:House,40|Bed,20|Mirror,10ecc。我想拆分字符串并"," "|"像这样删除:

VARIABLE A=House

VARIABLE A1=40


VARIABLE B=Bed

VARIABLE B1=20

VARIABLE C=Mirror

VARIABLE C1=10

我很抱歉我的英语。谢谢大家。

4

1 回答 1

1

试试这个:

如果您只想删除字符,则可以使用以下命令:

更新 3:

local myString = "House;home;flat,40|Bed;bunk,20|Mirror,10"
local myTable = {}
local tempTable = {}

local count_1 = 0
for word in string.gmatch(myString, "([^,|]+)") do
    myTable[#myTable+1]=word
    count_1=count_1+1
    tempTable[count_1] = {} -- Multi Dimensional Array
    local count_2 = 0
    for word_ in string.gmatch(myTable[#myTable], "([^,|,;]+)") do
        count_2=count_2+1
        local str_ = word_
        tempTable[count_1][count_2] = str_
        --print(count_1.."|"..count_2.."|"..str_)
    end
end

print("------------------------")
local myTable = {} -- Resetting my table, just for using it again :)
for i=1,count_1 do
    for j=1,#tempTable[i] do
        print("tempTable["..i.."]["..j.."] = "..tempTable[i][j])
        if(j==1)then myTable[i] = tempTable[i][j] end
    end
end

print("------------------------")
for i=1,#myTable do
    print("myTable["..i.."] = "..myTable[i])
end
--[[ So now you will have a multidimensional array tempTable with
     elements as:
        tempTable = {{House,home,flat},
                     {40},
                     {Bed,bunk},
                     {20},
                     {Mirror},
                     {10}}

     So you can simply take any random/desired value from each.
     I am taking any of the 3 from the string "House,home,flat" and
     assigning it to var1 below:
--]]

var1 = tempTable[1][math.random(3)]
print("var1 ="..var1)

-- So, as per your need, you can check var1 as: 
for i=1,#tempTable[1] do  -- #tempTable[1] means the count of array 'tempTable[1]'
  if(var1==tempTable[1][i])then
    print("Ok")
    break;
  end
end

----------------------------------------------------------------
   -- Here you can print myTable(if needed) --
----------------------------------------------------------------
for i=1,#myTable do
  print("myTable["..i.."]="..myTable[i])
end

--[[ The output is as follows:
    myTable[1]=House
    myTable[2]=40
    myTable[3]=Bed
    myTable[4]=20
    myTable[5]=Mirror
    myTable[6]=10

    Is it is what you are looking for..?
 ]]--

 ----------------------------------------------------------------

继续编码...... :)

于 2013-10-20T06:41:36.860 回答