[Rules DSL] How to retrieve the last line of a multi-line string

I am using OH3.1 to monitor a printer with the exec binding and I woud like to process the output of an lpstat command in a DSL rule.

Thing exec:command:controllaElia "Controlla Elia" [
	command="lpstat -p ET-2600",
    interval=0

I retrieve the output of the command on a string item

String EliaStr " Output" {channel="exec:command:controllaElia:output"}

The string output may be one or two lines and I would like to process it in a DSL rule with the following expression

val String str=(EliaStr.state.toString.split('\n') )

but it complains Type mismatch: cannot convert from String[] to String.
It seems to me that it may be related to the fact that arrays are not available in Xtend.
In some old posts I saw suggestions to use .get(i) but

val String str=(EliaStr.state.toString.get(0).split('\n') )

but it gives as error the method get(int) is undefined for type String (But wasn’t it previously defined type String[]?)

Is there a way in rule DSL or should I use a Javascript transform?
Thank you for your attention

Did you try it in a different sequence ?
.toString should return a string and I think thus does not know anything about get.
.split() should return an array of strings and thus should be aware of get().
I did not try it and I am not an expert with this stuff.

So shouldn’t it be similar to:

val String str=(EliaStr.state.toString.split('\n').get(0) )
1 Like
val parts=EliaStr.state.toString.split('\n')
logInfo("test", "There are " + parts.size + " parts")
logInfo("test", "The first part: " + parts.get(0))
logInfo("test", "The last part: " + parts.get(parts.size-1))
1 Like

Thank you. So the key point was to not specify String after val. I wonder what kind of type is the parts variable.

see

Ah ok, so the right declaration should have been

val String[] str = ...

In the end, this is what the error was telling. Thanks for your kind help.

No, it was telling you that you cannot .get() from a string before you split() it. It’s important to do things in the right order.

But as general advice in DSL, don’t type anything unless you have to. DSL will sort it out.