Jump to content


This is a ready-only archive of the InstallSite Forum. You cannot post any new content here. / Dies ist ein Archiv des InstallSite Forums. Hier können keine neuen Beiträge veröffentlicht werden.
Photo

Parsing Name/Value Pairs


2 replies to this topic

DeanNapper

DeanNapper
  • Full Members
  • 6 posts

Posted 28 March 2007 - 15:45

Does anyone know how I would go about parsing a name/value pair string?

I have the following name/value pair string:

sString = "Name=Fred Bloggs;Age=23;Sex=M";

I would like to write an InstallScript function which would allow me to get the the value of a given item e.g.

sAge = GetValueFromString("Age", sString);

Does anyone know how I'd go about doing this in InstallScript?



Stefan Krueger

Stefan Krueger

    InstallSite.org

  • Administrators
  • 13,269 posts

Posted 28 March 2007 - 16:11

Use string functions to find the ; and later to find the = sign.

DeanNapper

DeanNapper
  • Full Members
  • 6 posts

Posted 29 March 2007 - 16:59

This site has helped me so much over the years, I thought I'd share the function I created with the community:

function STRING GetNamedValueFromString(sName, sNameValueString)
NUMBER nNameValuePairListID, nNameAndValueListID, nResult, nRetval;
STRING sNameValuePair, sNameValue, sValue;
begin
nNameValuePairListID = ListCreate(STRINGLIST);
StrGetTokens(nNameValuePairListID, sNameValueString, ";");
nResult = ListGetFirstString (nNameValuePairListID, sNameValuePair);
while (nResult != END_OF_LIST)
nNameAndValueListID = ListCreate(STRINGLIST);
StrGetTokens(nNameAndValueListID, sNameValuePair, "=");
ListGetFirstString (nNameAndValueListID, sNameValue);
if sNameValue = sName then
ListGetNextString (nNameAndValueListID, sValue);
// Exit the loop
nResult = END_OF_LIST;
else
ListGetNextString (nNameValuePairListID, sNameValuePair);
endif;
ListDestroy(nNameAndValueListID);
endwhile;
ListDestroy(nNameValuePairListID);

return sValue;
end;

Edited by DeanNapper, 29 March 2007 - 16:59.