Storing User Input in Tables/Arrays

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Storing User Input in Tables/Arrays

Jorge Eduardo
Hi, guys!

I know it sounds noob, but it's noob !! LOL!
I'm studing Lua Programming for a few weeks...

The question is:
How do I store user input in a table/array?

For example, information from a school report card:

Name Grade_1 Grade_2 Average_Grade

John    7.0        8.0             7.5

I'm worried about this.
Thanks a lot for the help!

Reply | Threaded
Open this post in threaded view
|

Re: Storing User Input in Tables/Arrays

Andrew Starks-2
On Fri, May 5, 2017 at 9:14 AM, Jorge Eduardo de Araújo Oliveira
<[hidden email]> wrote:

> Hi, guys!
>
> I know it sounds noob, but it's noob !! LOL!
> I'm studing Lua Programming for a few weeks...
>
> The question is:
> How do I store user input in a table/array?
>
> For example, information from a school report card:
>
> Name Grade_1 Grade_2 Average_Grade
>
> John    7.0        8.0             7.5
>
> I'm worried about this.
> Thanks a lot for the help!
>

Tables are supremely flexible, able to store any and all value types
in the key or the value (except that nil cannot be a key).

So, in your example, we can take any one of multiple approaches,
depending upon what it is you want to accomplish! For example:

```
data = {John = {Grade_1 = 7.0, Grade_2 = 8.0, Average_Grade = 7.5}}
```

Here, the value of `data.John` is a table with three fields: Grade_1,
Grade_2 and Average_Grade

But what if there's more than one John? This won't work if you have
two Johns. Instead of indexing on the name, you might choose some
unique identifier, like their student ID number. But failing that, you
can change the model so that you store all students in an array.

data = {
  {Name = "John", Grade_1 = 7.0, Grade_2 = 8.0, Average_Grade = 7.5}
}

Now data can behaves like an array, complete with a length operator.
This table has one array element at index 1.

```
print(#data) --the length of data, aka the number of records.
--> 1
print(data[1].Name) --the value of the `Name` field of the first element.
--> John
```

There are about 500 other ways that you can store this data. Also, you
can add behaviors, such as having the Average_Grade field calculate
itself.

Hope this helps!

-Andrew





--
Andrew Starks
612 840 2939
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Storing User Input in Tables/Arrays

Martin
In reply to this post by Jorge Eduardo
On 05/05/2017 07:14 AM, Jorge Eduardo de Araújo Oliveira wrote:

> Hi, guys!
>
> I know it sounds noob, but it's noob !! LOL!
> I'm studing Lua Programming for a few weeks...
>
> The question is:
> How do I store user input in a table/array?
>
> For example, information from a school report card:
>
> Name Grade_1 Grade_2 Average_Grade
>
> John    7.0        8.0             7.5
>
> I'm worried about this.
> Thanks a lot for the help!
>

Hi,

as Andrew Starks pointed, first you mush decide how data structure
should look like. Probably you need just a sequence of records, where
each record is set of string fields "name", "grade_1", "grade_2",
"avg_grade".

Then you just create table for storing them:

  local data = {}

And populate it with records:

  local record = {}
  record.name = get_name()
  record.grade_1 = get_grade_1()
  ...
  data[#data + 1] = record

Alternatively, you may create each record at once:

  local record = {
    name = get_name(),
    grade_1 = get_grade_1(),
    grade_2 = get_grade_2(),
    avg_grade = get_avg_grade(),
  }
  data[#data + 1] = record

Then you may iterate <data> to get (or modify or even add/remove) it's data:

  for i = 1, #data do
    local rec = data[i]
    print(('[%d] name: %s, grade_1: %s, grade_2: %s, avg_grade:
%s'):format(i, rec.name, rec.grade_1, rec_grade_2, rec.avg_grade))
  end

Tables (along with functions) is crucial Lua language part. It's power
mostly relies on them. I recommend to read about them in Reference
Manual to understand all possible usages.

-- Martin

Reply | Threaded
Open this post in threaded view
|

Re: Storing User Input in Tables/Arrays

Sean Conner
In reply to this post by Jorge Eduardo
It was thus said that the Great Jorge Eduardo de Araújo Oliveira once stated:

> Hi, guys!
>
> I know it sounds noob, but it's noob !! LOL!
> I'm studing Lua Programming for a few weeks...
>
> The question is:
> How do I store user input in a table/array?
>
> For example, information from a school report card:
>
> Name Grade_1 Grade_2 Average_Grade
>
> John    7.0        8.0             7.5
>
> I'm worried about this.
> Thanks a lot for the help!

  I would arrange for the data to be stored as:

  data =
  {
    { name = "John" , grades = { 7.0 , 8.0 } } ,
    { name = "Sean" , grades = { 7.1 , 7.9 , 6.9 , 8.1 } } ,
    { name = "Sion" , grades = { 6.9 , 8.1 , 8.0 } } ,
    { name = "Jean" , grades = { 7.0 , 8.2 , 7.5 } } ,
  }

  I would not include the average, but make a function to calculate it from
a list of grades.  

  -spc (Using an array for the grades is nice because it can be expanded to
        include more than just two grades)