11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The following examples also returns a null string, because the specified substrings do notexist:SET colorlist="Red,Green,Blue,Yellow,Orange,Black"SET extract=$PIECE(colorlist,",",0)WRITE !,"Length=",$LENGTH(extract),!,"Value=",extractSET colorlist="Red,Green,Blue,Yellow,Orange,Black"SET extract=$PIECE(colorlist,",",8,20)WRITE !,"Length=",$LENGTH(extract),!,"Value=",extractNested $PIECE OperationsTo perform complex extractions, you can nest $PIECE references within each other. Theinner $PIECE returns a substring that is operated on by the outer $PIECE. Each $PIECEuses its own delimiter. For example, the following returns the state abbreviation “MA”:SET patient="John Jones/29 River St./Boston MA 02095"SET patientstateaddress=$PIECE($PIECE(patient,"/",3)," ",2)WRITE patientstateaddressThe following is another example of nested $PIECE operations, using a hierarchy of delimiters.First, the inner $PIECE uses the circumflex (^) delimiter to find the second piece ofnestlist: "A,B,C". Then the outer $PIECE uses the comma (,) delimiter to return the first andsecond pieces ("A,B") of the substring "A,B,C":SET nestlist="1,2,3^A,B,C^@#!"WRITE $PIECE($PIECE(nestlist,"^",2),",",1,2)Using $PIECE to the Left of the Equals SignWhen making assignments with the SET command, you can use $PIECE to the left, as wellas to the right, of the equals sign. When used to the left of the equals sign, $PIECE designatesa substring to be replaced by the assigned value. The only restriction is that the expressionparameter evaluate to a valid variable name. It cannot be a general expression.The use of $PIECE (and $EXTRACT) in this context differs from other standard functionsbecause it modifies an existing value, instead of just returning a value. The following examplechanges the value of colorlist to "Red,Green,Cyan,Yellow,Orange,Black":SET colorlist="Red,Green,Blue,Yellow,Orange,Black"WRITE !,colorlistSET $PIECE(colorlist,",",3)="Cyan"WRITE !,colorlist$PIECEIf $PIECE specifies more occurrences of the delimiter than exist in the target variable, itappends additional delimiters to the end of the value, up to one less than the specified number.The following example changes the value of smallcolor to "Green;Blue;;Red". The number<strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong> 285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!