Help retrieving Strings from Tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
xn1
Prole
Posts: 2
Joined: Wed Aug 12, 2020 12:01 am

Help retrieving Strings from Tables

Post by xn1 »

Hello, I'm new to Love 2D and LUA in general, so my question probably has a simple answer.
I seem to be having some trouble retrieving the value of strings previously stored in a table.
For example, let say I have something like this:

Code: Select all

    test = {"String"}
    a = string.sub(test[1],1,-1)
    print(a)
this code seems to run just fine in other LUA interpreters,
but when I try it on Love 2D I get an error

Image

or let say I have this other code:

Code: Select all

    test = {"index"}
    test2 = {}
    test2[test[1]] = "text"
    print(test2.index)
again, this compiles, but not in Love 2D

Image

It also appears that trying to print a string from a table to the console gives out a pointer to the table instead of the actual value.

Code: Select all

    test = {"index"}
    test2 = {}
    test2[test[1]] = "text"
    print(test[1])
    print(test2.index)
that code should output something like this
but in love 2D I get this:

Image

My question is. How do I get love 2D to give me the actual string, or at least to get example #2 to work?

Bonus question, I'm writing a simple csv parser for my project, but it looks like love2d doesn't recognize the file handler object from
io.open(). I get this error

Image

Do I have to use Love 2Ds own file reading functions? Or is there something else I'm doing wrong.
This is the code for my parser thus far:

Code: Select all

function loadcsv(path)
  local OutTable = {}
  local file = io.open(path, "r")
  local linecount = 0

  for line in file:lines() do
    local data = {}
    local headers = {}
    local headerkey = 1

    if linecount < 1 then
      for val in line:gmatch("([^,]+),?") do
        table.insert(headers, val)
      end
    else
      for word in line:gmatch("([^,]+),?") do
        data[headers[headerkey]] = word
        headerkey = headerkey + 1
        table.insert(OutTable, data)
      end
    end

    linecount = linecount + 1
  end

  file:close()
  return OutTable
end
the Idea is that I can give it a csv file like this one

Code: Select all

PropertyKey1,Propertykey2,Propertykey3
object1property1,object1property2,object1property3
object2property1,object2property2,object2property3
object3property1,object3property2,object3property3
and maker it into a table along this lines:

Code: Select all

objects = {
    {
      PropertyKey1 = object1property1
      PropertyKey2 = object1property2
      PropertyKey3 = object1property3
    }
    {
      PropertyKey1 = object2property1
      PropertyKey2 = object2property2
      PropertyKey3 = object2property3
    }
    {
      PropertyKey1 = object3property1
      PropertyKey2 = object3property2
      PropertyKey3 = object3property3
    }
}
Thank you all in advance
User avatar
CogentInvalid
Prole
Posts: 27
Joined: Sat Dec 14, 2013 12:15 am

Re: Help retrieving Strings from Tables

Post by CogentInvalid »

The code you posted works fine when I run it in Love2D. My only guess is that there's something elsewhere in your code that's messing with table indexing.
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help retrieving Strings from Tables

Post by pgimeno »

Yeah, that's obviously not the whole story. A 4-line program can't give an error in line 20, for example. Something else must be going on in some place that we haven't been shown.

The test cases work for me too:

Code: Select all

$ cat main.lua
    test = {"String"}
    a = string.sub(test[1],1,-1)
    print(a)

    test = {"index"}
    test2 = {}
    test2[test[1]] = "text"
    print(test2.index)

    test = {"index"}
    test2 = {}
    test2[test[1]] = "text"
    print(test[1])
    print(test2.index)
$ love11 .
String
text
index
text
xn1
Prole
Posts: 2
Joined: Wed Aug 12, 2020 12:01 am

Re: Help retrieving Strings from Tables

Post by xn1 »

Thanks for your replies. The problem turned out to not be the code. I was using Atom with the Love IDE extension. I tested all examples on an empty project, and it was still producing errors. However, a couple hours after I initially posted, Atom just dissapeared from my computer. I don't know why, but it just uninstalled itself while I wasn't looking. With Atom gone, I tried out the same code that was causing the errors again, and it ran with no issues. I have no clue as to what the problem actually was. I was afraid that more errors might pop up in the future, so I erased the remnants atom, and reinstalled both LUA and Love 2D just to be safe.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], rabbitboots and 3 guests