Что нового

Сложение данных из массива

Isn

Новичок
Сообщения
119
Репутация
2
Доброго времени суток:
Как можно сложить данные online и inbattles из массива:
Получив в итоге:
online = допустим 30 173
inbattles = допустим 19 738
Думаю что нужно использовать StringRegExp но не знаю как
Код:
{"total":17694797,"nodes":{"c13.tankionline.com":{"load":"50","online":1748,"inbattles":1288},"c24.tankionline.com":{"load":"50","online":162,"inbattles":104},"c10.tankionline.com":{"load":"50","online":2082,"inbattles":1335},"c20.tankionline.com":{"load":"50","online":223,"inbattles":127},"c21.tankionline.com":{"load":"50","online":165,"inbattles":103},"c15.tankionline.com":{"load":"50","online":336,"inbattles":199},"c16.tankionline.com":{"load":"50","online":276,"inbattles":177},"c12.tankionline.com":{"load":"50","online":1617,"inbattles":1188},"c25.tankionline.com":{"load":"50","online":215,"inbattles":138},"c9.tankionline.com":{"load":"50","online":1953,"inbattles":1341},"c19.tankionline.com":{"load":"50","online":209,"inbattles":125},"c2.tankionline.com":{"load":"50","online":2838,"inbattles":1733},"c4.tankionline.com":{"load":"50","online":1877,"inbattles":1142},"i1.tankionline.com":{"load":"50","online":0,"inbattles":0},"c5.tankionline.com":{"load":"50","online":1568,"inbattles":992},"c11.tankionline.com":{"load":"50","online":1807,"inbattles":1238},"c23.tankionline.com":{"load":"50","online":146,"inbattles":95},"c18.tankionline.com":{"load":"50","online":226,"inbattles":125},"c8.tankionline.com":{"load":"50","online":1952,"inbattles":1314},"c1.tankionline.com":{"load":"50","online":2204,"inbattles":1290},"c7.tankionline.com":{"load":"50","online":2018,"inbattles":1282},"c17.tankionline.com":{"load":"50","online":222,"inbattles":125},"c14.tankionline.com":{"load":"50","online":1645,"inbattles":1104},"c3.tankionline.com":{"load":"50","online":2340,"inbattles":1407},"c6.tankionline.com":{"load":"50","online":1847,"inbattles":1235},"c22.tankionline.com":{"load":"50","online":143,"inbattles":96}}}
 

inververs

AutoIT Гуру
Сообщения
2,135
Репутация
465
Код:
$sData = '{"load":"50","online":1748,"inbattles":1288},"c24.tankionline.com":{"load":"50","online":162,"inbattles":104}'
$aOnline = StringRegExp($sData,'(?<="online":)\d+',3)
$iOnlineSumm = 0
For $sRow In $aOnline
	$iOnlineSumm+=$sRow
Next
ConsoleWrite('> Online: '& $iOnlineSumm & @LF)
$aInbattles = StringRegExp($sData,'(?<="inbattles":)\d+',3)
$iInbattlesSumm = 0
For $sRow In $aInbattles
	$iInbattlesSumm+=$sRow
Next
ConsoleWrite('> Inbattles: '&$iInbattlesSumm & @LF)
 
Автор
Isn

Isn

Новичок
Сообщения
119
Репутация
2
То что нужно.
Спс inververs
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,320
Isn,
Или так.
Код:
$sJson = FileRead(@ScriptDir & '\test.txt')
Dim $aKeys[2] = ['online', 'inbattles']

For $i = 0 To 1
	$iSumm = 0
	;$aTmp = StringRegExp($sJson, '"' & $aKeys[$i] & '\D+(\d+)\D', 3)
	$aTmp = StringRegExp($sJson, '"' & $aKeys[$i] & '":"?(.+?)"?(?=,|\})', 3)
	If @error Then ContinueLoop
	For $j = 0 To UBound($aTmp) - 1
		$iSumm += $aTmp[$j]
	Next
	ConsoleWrite(StringFormat('summ %-10s=\t%d\n', $aKeys[$i], $iSumm))
Next
 

inververs

AutoIT Гуру
Сообщения
2,135
Репутация
465
madmasles
:

Это работает, но как бы не совсем логично "искать цифры которые окружены не цифрами"
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,320
inververs [?]
но как бы не совсем логично "искать цифры которые окружены не цифрами"
ИМХО, в конкретном данном случае, логично.
 
Верх