Hello everyone I am trying to find a relationship between two tables and I am finding it very difficult.
The first table is a set of values which consist of primarily values that will be used for WalkSpeed.
1 | local tab = { 10 , 20 , 30 , 40 , 50 } |
The second table is a set of values which consist of values primarily for MaxHealth.
1 | local tab 2 = { 5 , 10 , 15 , 20 , 25 } |
Is there a possible way of finding relationships between these two corresponding tables. Such as 10 and/or 20 appearing in both tables. Any help would be very much appreciated, thank you in advance.
01 | local Array 1 = { 10 , 20 , 30 , 40 , 50 } |
02 | local Array 2 = { 5 , 10 , 15 , 20 , 25 } |
03 |
04 | local ValuesInCommon = { } |
05 |
06 | for _,v in pairs (Array 1 ) do |
07 | for i = 1 ,#Array 2 do |
08 | if v = = Array 2 [ i ] then |
09 | ValuesInCommon [ #ValuesInCommon + 1 ] = v |
10 | end |
11 | end |
12 | end |
13 |
14 | print ( unpack (ValuesInCommon)) |
10 20
Here is why this works:
We start by creating an table to hold all values that are found to be in common. We then move into a for loop that will represent each Array value (v) separately. Because we are indexing each value separately - we can compare them one at a time to Array2, which is what the second for loop does. It will check if an Array1 value, v, is the same as any values from Array2, and if they are move them into the ValuesInCommon table.
You'll need to define what you're looking for first, only then can you look for it.