Search code examples
qore

get amount of items consistently from list or hash


I am parsing XML and am receiving either one hash or list of more hashes, see:

qore -l xml -nX 'parse_xml("<root> <row><id>1</id><name>foo</name></row> <row><id>2</id><name>bar</name></row></root>")'

qore -l xml -nX 'parse_xml("<root> <row><id>1</id><name>foo</name></row></root>")'

I need to know how many rows was parsed, but calling elements or .size() returns different values for list (amount of items) and hash (amount of keys!).

Is there a way ho to do it without the need to copy all values to new softlist variable?

hash xml = prase_xml("..."); softlist tmp = xml.root.row; # makes unnecessary copy! tmp.size();

Thanks O.


Solution

  • use the <value>::lsize() pseudo-method, which returns:

    • the number of elements of a list
    • 1 for all other values
    • 0 for NOTHING

    ex:

    hash<auto> xml = parse_xml(xmlstr);
    int size = xml.root.row.lsize();