Morrowind Mod:XStringParse

The UESPWiki – Your source for The Elder Scrolls since 1995
Jump to: navigation, search

A function added by MWSE.

xStringParse identifies and extracts data from the source string based on the pattern string. The first return value is a long indicating how much of the pattern matched successfully. The values matched by the pattern will then be returned in order. For example, the following statement will put value of 37 in the long variable lval, a value of 2.4 in the float variable fval, the string "Golden Egg" in the string variable sval, and the value 4 into matches to indicated that all of the pattern successfully matched. For example:

setx matches lval fval sval to xStringParse "%d %f %s" "37 2.4 Golden Egg"

Following command, however, will put the whole string in the string variable.

setx matches sval lval fval to xStringParse "%s %d %f" "GoldenEgg 37 2.4"

In the pattern string, all of the special features begin with a % symbol. Some, simply match special characters that can't otherwise be typed in the pattern string.

%% matches a single % sign.
%n      matches the new line marker, CR/LF.
%q      matches the " quotation mark.

The rest return the values matched so they can be stored in variables with setx.

%d matches a decimal integer and the value is returned as type long.
%h      matches a hexadecimal integer and the value is returned as type long.
%f      matches a real number and the value is returned as type float.
%s      matches the rest of the string and returns it as type string.
%l      matches exactly four characters and returns them as type long.

The uppercase forms %N, %Q, %D, %H, %F, %S, and %L also work. Only one of these takes a precision specifier, and it is best explained by examples:

setx matches sval lval to xStringParse "%.0s_%d" "gold_001"

In this case, the special pattern "%.0s" is followed by a "_" character, so only "gold" will be stored in the string sval, a value of 1 will be in lval, and matches will be 3. Matches is normally one more than the number of values returned to indicated that in addition to filling all of the variables the entire pattern was matched successfully. You could use a pattern simply for string comparison.

setx matches lval to xStringParse "%d Apples" somestring

If the somestring variable really holds the string "43 Apples" then matches will be 2 to indicate that the pattern was matched completely. If somestring were "47 Pears" then matches will be 1 to indicate that the first variable was filled (with 47) but that the rest of the pattern failed to match. If somestring were "Hello" then matches will be 0 to indicate that not even the first variable was filled correctly.

Syntax[edit]

count (long) ...: xStringParse pattern (string) source (string)

Example[edit]

Following code is replacement for command "set random_number to random var" since function "random" does not accept variables.

short digits
short count   ; variable that is used to get random number
long ones
long tens
long hundreds
short rn   ; random number
long temp
long countstr

set hundreds to 0
set tens to 0
if ( count >= 100 )
        set digits to 3 
elseif ( count >= 10 )
        set digits to 2
else
        set digits to 0
        set ones to count
endif
set temp to digits - 3
ifx ( temp )
else
        setx countstr to xStringBuild "%d" count
        setx hundreds to xStringBuild "%.1s" countstr
        setx temp hundreds to xStringParse "%d" hundreds
        setx tens to xStringBuild "%1.1s" countstr
        setx temp tens to xStringParse "%d" tens
        setx ones to xStringBuild "%2.1s" countstr
        setx temp ones to xStringParse "%d" ones
endif
set temp to digits - 2
ifx ( temp )
else
        setx countstr to xStringBuild "%d" count
        setx tens to xStringBuild "%.1s" countstr
        setx temp tens to xStringParse "%d" tens
        setx ones to xStringBuild "%1.1s" countstr
        setx temp ones to xStringParse "%d" ones
endif
if ( hundreds == 9 )
        set hundreds to random 901
elseif ( hundreds == 8 )
        set hundreds to random 801
elseif ( hundreds == 7 )
        set hundreds to random 701
elseif ( hundreds == 6 )
        set hundreds to random 601
elseif ( hundreds == 5 )
        set hundreds to random 501
elseif ( hundreds == 4 )
        set hundreds to random 401
elseif ( hundreds == 3 )
        set hundreds to random 301
elseif ( hundreds == 2 )
        set hundreds to random 201
elseif ( hundreds == 1 )
        set hundreds to random 101
endif
if ( tens == 9 )
        set tens to random 91
elseif ( tens == 8 )
        set tens to random 81
elseif ( tens == 7 )
        set tens to random 71
elseif ( tens == 6 )
        set tens to random 61
elseif ( tens == 5 )
        set tens to random 51
elseif ( tens == 4 )
        set tens to random 41
elseif ( tens == 3 )
        set tens to random 31
elseif ( tens == 2 )
        set tens to random 21
elseif ( tens == 1 )
        set tens to random 11
endif
if ( ones == 9 )
        set ones to random 10
elseif ( ones == 8 )
        set ones to random 9
elseif ( ones == 7 )
        set ones to random 8
elseif ( ones == 6 )
        set ones to random 7
elseif ( ones == 5 )
        set ones to random 6
elseif ( ones == 4 )
        set ones to random 5
elseif ( ones == 3 )
        set ones to random 4
elseif ( ones == 2 )
        set ones to random 3
elseif ( ones == 1 )
        set ones to random 2
endif
set rn to hundreds + tens + ones

See Also[edit]

xPCCellID
xRefID
xMyCellID
xGetName
xSetName
xGetBaseID
xStringCompare
xStringLength
xStringBuild
xStringMatch
xLogMessage
xMessageFix