Cocos2d-x (quick-lua) reads csv files

Source: Internet
Author: User

Cocos2d-x (quick-lua) reads csv files

1. Prepare a csv file

 

Bytes

Ii. string segmentation Functions

 

 

 function split(str, reps)    local resultStrsList = {};    string.gsub(str, '[^' .. reps ..']+', function(w) table.insert(resultStrsList, w) end );    return resultStrsList;end

Iii. parsing csv Functions

 

 

Function loadCsvFile (filePath) -- reads the File local data = cc. fileUtils: getInstance (): getStringFromFile (filePath); -- divide local lineStr = split (data, '') by line ,''); -- [save from row 3rd (the first row is the title, the second row is the comment, and the following row is the content) with a two-dimensional array: arr [ID] [attribute title string] local titles = split (lineStr [1],); local ID = 1; local arrs = {}; for I = 3, # lineStr, 1 do -- the content of each column in a row: local content = split (lineStr [I],); -- use the title as an index to save the content of each column, take the value as follows: arrs [1]. title arrs [ID] ={}; for j = 1, # titles, 1 do arrs [ID] [titles [j] = content [j]; end ID = ID + 1; end return arrs; end
The parsed data is stored in the array.

 

Iv. Test code

 

local function main()    local csvConfig = loadCsvFile(res/Mutou.Csv);        print(csvConfig[1].Name .. : .. csvConfig[1].Des);    print(csvConfig[2].Name .. : .. csvConfig[2].Des);end

In this way, the descriptions of the first and second rows of the csv file can be output. Is it much simpler than c ++. Okay, that's it!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.