Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
How to read lines and put them in an array? - Gideros Forum

How to read lines and put them in an array?

DarkPixelDarkPixel Member
edited September 2012 in General questions
Hey guys! Well I am trying to make a simple application, not a game. The application has a strings.txt file, in which each line as a URL.
I want to put each line in an array, and then download that link and view it as a bitmap.
This is my code for reading them an putting them in an array.
local i = 1
lines = {}
        for line in io.lines('strings.txt') do 
                   lines[i] = line
		   print(lines[i] ..  ' ' ..i)
		   i = i +1
 
        end
My issue is that the URL's are read with an additional blank line. So I can't download them. Only the last line is getting downloaded, cause it doesn't have an additional line.

Comments

  • talistalis Guru
    edited September 2012 Accepted Answer
    From your code i can not see anything wrong but you can remove the spaces programmatically before writing it to array i guess with lua string object.
    (What i mean is before writing the string value to array you are reading it to variable called line in your code)

    Maybe something like this:
    function trim(s)
      return (string:gsub("^%s*(.-)%s*$", "%1"))
    end
    By the way there are so many functions more follow here to experiment them.
    http://lua-users.org/wiki/CommonFunctions
    or
    http://lua-users.org/wiki/StringLibraryTutorial
  • DarkPixelDarkPixel Member
    edited September 2012
    Hey mate, thanks for your response. I tried your code but unfortunately it shows this error: "main.lua:26: calling 'gsub' on bad self (string expected, got table)"
    but I am sure that I pass a string to the function.

    I will checkout your links now to see if I can find anything. Thanks again. :)

    EDIT: It worked actually. I forgot to replace string in the trim function with s :D
  • gz job don't hesitate to ask any questions, when you have:D
  • Will do! Thanks again! :D
Sign In or Register to comment.