Question
· Apr 4, 2019

Remove last element from $lb property

How can I remove elements from $lb property? Specifically last element?
$list does not work with $lb properties.
$listupdate can't remove elements
The only solution I found is a temp variable and $list, but isn't there something better?

Class User.Test Extends %RegisteredObject
{

Property List As %List [ InitialExpression = {$lb(1,2,3)} ];

/// do ##class(User.Test).TestList()
ClassMethod TestList()
{
    set obj = ..%New()
    do obj.ClearLast()
    
    set list = obj.List
    zw list
}

Method ClearLast()
{
    //set $list(..List, *, *) = "" // <OBJECT DISPATCH>zTestList+2^User.Test.1 *Property 'List' in class 'User.Test' must be MultiDimensional
    //set ..List = $LISTUPDATE(..List, $ll(..List), "") // Works, but returns $lb(1,2,"")
    
    // works
    set list = ..List
    set class = $lg(list, *)
    set $list(list, *, *) = ""
    set ..List = list
}
Discussion (6)1
Log in or sign up to continue

I prefer this solution as it is in full accordance with the documentation and
with no need also for beginners to fiddle into internals .
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_flist

<snip>

  • $LIST(list,position,end) returns a “sublist” (an encoded list string) containing the elements of the list from the specified start position through the specified end position (inclusive). If position and end specify the same element, $LIST returns this element as an encoded list.

</snip> 

Hi.

Recently, well yesterday, I needed to do exactly the same , and on a class property as well! I found an answer more by accident than design:

s data=$LB(1,2,3)

s data=$LI(data,1,*-1)

zw data
data=$lb(1,2)

When I saw the other answer, I worried that this might not work when the result is only one item, but it does, as confirmed by the documentation for the $LIST function. If you supply all three parameters - list, position, end - then it always returns another list. I was pleasantly surprised!

Mike