I want to convert the lua string as shown in the example.

Asked 2 years ago, Updated 2 years ago, 62 views

I am writing a lua script for Yamaha router, but I am having trouble with string operation.
Could you please provide me with a script to edit the contents of the array as follows?

·Original data

{ANONYMOUS03, L2TP session not succeeded, start: April 24, 2022 18:48:19, contact: PC1001
, ANONYMOUS04, L2TP session not succeeded, start: April 25, 2022 12:38:39, contact: PC1002}

·The shape you want to process

{ANONYMOUS03="PC1001",ANONYMOUS04="PC1002"}
{ANONYMOUS03="April 24, 2022 18:48:19",ANONYMOUS04="April 25, 2022 12:38:39"}

◆Description
I would like to create an array of ANONYMOUS numbers and connecting names, ANONYMOUS numbers and starting times from the original data.

◆Tried code
First of all, in order to remove unnecessary elements for each array, I wrote the following code, but it didn't work.

--Remove unnecessary elements
fork, vin pairs (array) do
  if(string.find(v, "ANONYMOUS") or string.find(v, "Connected") == nil then
    table.remove(array,k)
  end
end

·The above print result
1 ANONYMOUS03
2 Status of last L2TP session:
3 Connected to: PC1001
4 ANONYMOUS04
5 Status of the last L2TP session:
6 Destination: PC1002

lua

2022-09-30 10:28

1 Answers

This is an example script that creates a new table by extracting what you need instead of deleting what you don't need.
The results are set to array_connection and array_starttime.

local array={
"ANONYMOUS03", "L2TP session not succeeded", "Start: April 24, 2022 18:48:19", "Connected to: PC1001",
"ANONYMOUS04", "L2TP session not succeeded", "Start: April 25, 2022 12:38:39", "Connected to: PC1002"
}
array_connection={}
array_starttime={}
fork, vin pairs (array) do
        from if(string.find(v, "ANONYMOUS") to = nil then
                anonymous=v
        end
        from if(string.find(v, "start")) to = nil then
                starttime=string.match(v, "Start:(.*)")
        end
        from if(string.find(v, "to whom") to = nil then
                connection=string.match(v, "Connected to: (.*)")
                array_connection [anonymous] = connection
                array_starttime [anonymous] = starttime
        end
end
print("==array_connection===========================")
fork, vin pairs (array_connection) do
        print("k=",k, "v=",v)
end
print("==array_starttime===========================")
fork, vin pairs (array_starttime) do
        print("k=",k, "v=",v)
end

Run Results

==array_connection===========================
k = ANONYMOUS04 v = PC1002
k = ANONYMOUS03 v = PC1001
==array_starttime===========================
k = ANONYMOUS04 v = April 25, 2022 12:38:39
k = ANONYMOUS03 v = April 24, 2022 18:48:19


2022-09-30 10:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.