Logic Machine Forum
Match nth item of a string - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Match nth item of a string (/showthread.php?tid=4455)



Match nth item of a string - Joep - 18.12.2022

I'm looking for a way to grab the value of the 3th parent and 2th child item of a string. Below an example of the data i get. The value i need is marked in red. It's not possible to use the id's as they change all the time. With css it's done like this item:nth-child(3) item:nth-child(2) but how is it done in Lua?

<values>
  <item id='0x8764bc'>
      <item id='0x87676c'>
        <value>32.5°C</value>
      </item>
      <item id='0x87de1c'>
        <value>28.6°C</value>
      </item>
      <item id='0x87de64'>
        <value>28.4°C</value>
      </item>
      <item id='0x876724'>
        <value>62.7°C</value>
      </item>
  </item>
  <item id='0x876504'>
      <item id='0x8b7ab4'>
        <value>Aan</value>
      </item>
      <item id='0x87e0d4'>
        <value>Aan</value>
      </item>
  </item>
  <item id='0x8765dc'>
      <item id='0x878554'>
        <value>Uit</value>
      </item>
      <item id='0x861b84'>
        <value>Aan</value>
      </item>
      <item id='0x861bcc'>
        <value>Aan</value>
      </item>
      <item id='0x87850c'>
        <value>Aan</value>
      </item>
  </item>
</values>


RE: Match nth item of a string - admin - 20.12.2022

It makes more sense to find values by ID instead of position inside of the XML that can potentially change. Try this:
Code:
data = [[
<values>
  <item id='0x8764bc'>
      <item id='0x87676c'>
        <value>32.5°C</value>
      </item>
      <item id='0x87de1c'>
        <value>28.6°C</value>
      </item>
      <item id='0x87de64'>
        <value>28.4°C</value>
      </item>
      <item id='0x876724'>
        <value>62.7°C</value>
      </item>
  </item>
  <item id='0x876504'>
      <item id='0x8b7ab4'>
        <value>Aan</value>
      </item>
      <item id='0x87e0d4'>
        <value>Aan</value>
      </item>
  </item>
  <item id='0x8765dc'>
      <item id='0x878554'>
        <value>Uit</value>
      </item>
      <item id='0x861b84'>
        <value>Aan</value>
      </item>
      <item id='0x861bcc'>
        <value>Aan</value>
      </item>
      <item id='0x87850c'>
        <value>Aan</value>
      </item>
  </item>
</values>]]

function starttag(p, tag, attrs)
  if tag == 'item' then
    id = attrs.id
  elseif tag == 'value' then
    value = true
  end
end

function endtag(p, tag)
  if tag == 'item' then
    id = nil
  elseif tag == 'value' then
    value = nil
  end
end

function text(p, tdata)
  if value and id then
    values[ id ] = tdata
    value = nil
  end
end

values = {}

require('lxp').new({
  StartElement = starttag,
  EndElement = endtag,
  CharacterData = text,
}):parse(data)

log(values)



RE: Match nth item of a string - Joep - 20.12.2022

With this function i got a nice result of the items with their values but as the ID's change with every new connection made i can't use them.
Also the sorting is different using this function and with the raw data it's not. Below to examples with different ID's as you can see.

* table:
["0xe9fd3c"]
* string: 3144h
["0xe9a164"]
* string: 0.01 V
["0xde6bb4"]
* string: Aan
["0xe5a494"]
* string: Verwarmen
...


* table:
["0xee5dc4"]
* string: 3144h
["0xeb74cc"]
* string: 31 Hz
["0xebf184"]
* string: 1070
["0xf1a3f4"]
* string: 17593.1 kWh
...


RE: Match nth item of a string - Joep - 20.12.2022

This one is solved by Erwin van der Zwart already. He send me this solution that create a table with sorted numbering.

function text(p, tdata)
  if value and id then
table.insert(values, tdata)
    value = nil
  end
end