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?
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.
Parsing Name/Value Pairs
Started by
DeanNapper
, Mar 28 2007 15:45
2 replies to this topic
Posted 28 March 2007 - 16:11
Use string functions to find the ; and later to find the = sign.
Stefan Krüger
InstallSite.org twitter facebook
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;
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.