Lewis Greitzer · Aug 28, 2017

padding string in BPL

Hi folks, I'm trying to pad my patient MRN to 10 characters before I do a SQL lookup in my BPL. I've tried various approaches, and have not been successful.

When I add a "code" statement with the following code, I get an error as below.

set context.NewID = "0000000000"
set $EXTRACT(context.NewID,10-len(context.PatientID)) = context.PatientID
ERROR <Ens>ErrException: <OBJECT DISPATCH>zS8+5 ^CHSLI.BPLQueryDatabaseforPatientID.Thread1.1 *Property 'NewID'
in class 'CHSLI.BPLQueryDatabaseforPatientID.Context'
must be MultiDimensional -- logged as '-'
number - @'
set $EXTRACT(context.NewID,10-len(context.PatientID)) = context.PatientID'

Any ideas/help would be appreciated.


0 750
Discussion (9)1
Log in or sign up to continue

$EXCTRACT()  and also $LI(), $Piece()
is not allowed at the left side of SET when the target is an object property

use an intermediate variable instead.


set tmp="0000000000"
set $EXTRACT(tmp,10-len(context.PatientID)) = context.PatientID
set context.NewID=tmp

it might be easier that way

set context.NewID=$EXTRACT("0000000000"_context.PatientID,*-9,*)

you missed the $TRANSLATE(.....," ",0) around

pls. set the hook to your preferred solution

I like this approach. Didn't know I could call the Method directly from the BPL. Thanks.

Provided your patient MRN doesn't contain whitespaces, you can use $justify to pad string to required length and $translate to convert whitespaces into zeros.

w $tr($j("a", 10), " ", 0)

COS shorthand is hard to read in browser;
I personally prefer full size COS + Courier as font.

Cache dies not like an object property as a left side argument,

but you can solve the problem much simpler:

set context.NewID=$tr($j(context.PatientID,10)," ",0)


By the way, if your solution would work, the result would be longer then 10 chars

for example, if context.PatientID=123 then you

would get: "000000123000"

instead of: "0000000123"



The second approach (set context.NewID=$EXTRACT("0000000000"_context.PatientID,*-9,*)) worked great. When I checked the documentation for the $JUSTIFY, it doesn't say you can specify your pad character.

Looked like only spaces to me: $JUSTIFY(expression,width[,decimal])

I'm not exactly clear on which you intend to hold the new ID (context.newID maybe by the name?)

Regardless, you can also use the built-in pad method:

set context.NewID = ##class(Ens.Rule.FunctionSet).Pad(context.PatientID,-10,0)

where context.PatientID holds your original Patient ID. -10 means you want to prepend to be 10 characters, and "0" is the character you want prepended.