Tables: initializing elements

Discuss any general programming issues here
Post Reply
User avatar
r-tea
Posts: 123
Joined: Tue Feb 16, 2016 11:48 pm
Location: Zdzieszowice, Poland
Contact:

Tables: initializing elements

Post by r-tea » Wed Aug 09, 2017 9:59 pm

I need to get a time string, for example: "21:52:20", splitted into a tgree element table.

Code: Select all

    Function UpdateTime()
        time = {hour="00", mins="00", secs="00"}
        time = SplitStr(GetTime(True),":")
        If time["secs"]= "20" Then mui.Set("sl_min", "value", "20")
    EndFunction 
I get error: "Table field "secs" was not initialized!" pointing the "If" line.
As you can see all three fields were initialized in the constructor:
time = {hour="00", mins="00", secs="00"}

peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: Tables: initializing elements

Post by peceha » Thu Aug 10, 2017 9:05 am

Hi,
you get an error because in following line:

Code: Select all

time = SplitStr(GetTime(True),":")
your "time" variable (table) gets changed

Before it was: {hour="00,mins="00",secs="00"}
after it becames: {"00","00","00"} - or whatever comes back from "getTime()" function
There are no more strings as indices - only integers (0,1,2)

User avatar
r-tea
Posts: 123
Joined: Tue Feb 16, 2016 11:48 pm
Location: Zdzieszowice, Poland
Contact:

Re: Tables: initializing elements

Post by r-tea » Thu Aug 10, 2017 7:40 pm

It doesn't make sense. Do you want to say that SplitStr(GetTime(True),":") considers 0, 1 and 2 elements as already occupied and expects another three: 3, 4 and 5?

peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: Tables: initializing elements

Post by peceha » Thu Aug 10, 2017 8:19 pm

The first time you make a "table" it gets 3 elements with the indices: "hour" "mins" "secs" and NOT: 0,1,2
Try this:

Code: Select all

foreach({hour="00", mins="00", secs="00"}, debugprint)
Now, after "getTime" command your original table gets overwritten with a new one:

Code: Select all

foreach(SplitStr(GetTime(True),":"),debugprint)
this one doesn't have "hour" "mins" "secs" indices anymore BUT HAS : 0,1,2

Post Reply