Question
Sehinde Raji · May 21

How to extract numbers from a numerical stream using Cache Objectscript

Hey I am very new to Intersystems Objectscript and I am trying to find a way of extract 4 digit numbers from a stream.

I have a stream full of results 

2334

3434

5543

4334

 

And I am trying to remove each 4 digit grouping from the stream.

I have tried the following for loop 

for i=1:1:$length(result) {

set curr = $extract(result, i)

write !, curr

set count(curr) = $get(count(curr)) + 1

}

 

Unfortunately the loop extracts one digit.

Does anyone have any suggestions on how to solve this ?

Product version: IRIS 2020.1
00
1 0 6 111
Log in or sign up to continue

What's your linefeed symbol?

Also you have a string, not a stream.

Anyway, try this:

set nl = $$$NL
set str = "2334" _ nl _ "3434" _ nl _ "5543" _ nl _ "4334"
for i = 1:1: $length(str, nl) {
    set value = $piece(str, nl, i)
    write value, !
}

If there aren't any delimiters in your input string, then this will do it:

set result="233434345543433400110"
for i=1:4:$LENGTH(result) {
    write $E(result,i,i+3),!    
}

Output:

2334
3434
5543
4334
0011
0

You have a string of digits... like

set result="12345678900987654321"

then you can easily extract groups of four digits as

for i=1:4:$length(result) write $extract(result,i,i+3),!

this gives you

1234
5678
9009
8765
4321

assuming, there are no other characters between those numbers...