Regex oder StrSplit?

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

mINXZKA
Posts: 8
Joined: 16 Mar 2016, 03:47

Regex oder StrSplit?

Post by mINXZKA » 24 Jul 2020, 09:02

Hi,

ich hole mir mit der Funktion DownloadToString Werte einer URL. Leider gibt es keinen XML / JSON output. Deshalb bin ich gezwungen anders an die Daten zu kommen.

Das ist der Output in einem String

Code: Select all

Server's local time = Fri Jul 24 15:46:16 CEST 2020
HTTP status = Alive
_data status = Alive
_data processed requests = 124525
_data queued jobs = 0
_data queued responses = 0
_data total errors = 0
_data running profiles = 0
Total memory = 8142848 KB
Free memory = 6128958 KB
Max memory = 8142848 KB
Used memory = 24% used
getCommittedVirtualMemorySize = 9225891840
getFreePhysicalMemorySize = 1463484416
getFreeSwapSpaceSize = 3257401344
getProcessCpuLoad = 0.0
getProcessCpuTime = 2057859375000
getSystemCpuLoad = 0.0032399702888670223
getTotalPhysicalMemorySize = 12884430848
getTotalSwapSpaceSize = 14830587904
available Processors = 2
operating system = amd64
system load = -1.0
Was wäre die Sinnvollste Möglichkeit an gewisse Informationen hinter dem = zu kommen? Z.b. wenn ich Free memory haben möchte.
Sollte man da RegEx benutzen, mehrfach StrSplit oder gibts was besseres?

Danke

swagfag
Posts: 3976
Joined: 11 Jan 2017, 17:59

Re: Regex oder StrSplit?

Post by swagfag » 24 Jul 2020, 09:19

Code: Select all

str =
(`%
Server's local time = Fri Jul 24 15:46:16 CEST 2020
HTTP status = Alive
_data status = Alive
_data processed requests = 124525
_data queued jobs = 0
_data queued responses = 0
_data total errors = 0
_data running profiles = 0
Total memory = 8142848 KB
Free memory = 6128958 KB
Max memory = 8142848 KB
Used memory = 24% used
getCommittedVirtualMemorySize = 9225891840
getFreePhysicalMemorySize = 1463484416
getFreeSwapSpaceSize = 3257401344
getProcessCpuLoad = 0.0
getProcessCpuTime = 2057859375000
getSystemCpuLoad = 0.0032399702888670223
getTotalPhysicalMemorySize = 12884430848
getTotalSwapSpaceSize = 14830587904
available Processors = 2
operating system = amd64
system load = -1.0
)

Obj := Object(StrSplit(str, ["=", "`n"], "`r ")*)
MsgBox % Obj["Server's local time"]
  . "`n" Obj["HTTP status"]
  . "`n" Obj["_data status"]
  . "`n" Obj["_data processed requests"]
  . "`n" Obj["_data queued jobs"]
  . "`n" Obj["_data queued responses"]
  . "`n" Obj["_data total errors"]
  . "`n" Obj["_data running profiles"]
  . "`n" Obj["Total memory"]
  . "`n" Obj["Free memory"]
  . "`n" Obj["Max memory"]
  . "`n" Obj["Used memory"]
  . "`n" Obj["getCommittedVirtualMemorySize"]
  . "`n" Obj["getFreePhysicalMemorySize"]
  . "`n" Obj["getFreeSwapSpaceSize"]
  . "`n" Obj["getProcessCpuLoad"]
  . "`n" Obj["getProcessCpuTime"]
  . "`n" Obj["getSystemCpuLoad"]
  . "`n" Obj["getTotalPhysicalMemorySize"]
  . "`n" Obj["getTotalSwapSpaceSize"]
  . "`n" Obj["available Processors"]
  . "`n" Obj["operating system"]
  . "`n" Obj["system load"]

mINXZKA
Posts: 8
Joined: 16 Mar 2016, 03:47

Re: Regex oder StrSplit?

Post by mINXZKA » 24 Jul 2020, 09:30

Code: Select all

site := DownloadToString(url)
Obj := Object(StrSplit(site, ["=", "`n"], "`r ")*)
MsgBox % Obj["Free memory"]
Dein Output war leider leer.

Ich hab ein python script gefunden und das mal nachgebaut. Das hat funktioniert.

Code: Select all

site := DownloadToString(url)

for k, v in GetJspData(site)
	if (k = "Free memory")
		MsgBox % v


GetJspData(Site)
{
	Data := {}
	GetLines := StrSplit(Site, "`r`n")
	for key, Line in GetLines
	{
		if (Line != "")
		{
			Split := StrSplit(Line, " = ")
			Data[Split[1]] := Split[2]
		}
	}
	return Data
}

swagfag
Posts: 3976
Joined: 11 Jan 2017, 17:59

Re: Regex oder StrSplit?

Post by swagfag » 24 Jul 2020, 10:56

then ure either using an oldoldoldold ahk version(one, which doesnt support variadic calls); or ur DownloadToString() returns a string formatted differently than the one uve shown here; or u got some other error in ur script
if u run the hardcoded string sample on its own, ull see it shows the correct result

Post Reply

Return to “Ich brauche Hilfe”