# Reverse Read

**URL:** <https://techcommunity.softwareag.com/t/reverse-read/75746>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [November 26, 2007, 12:07pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746 "2007-11-26T12:07:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcs](https://avatars.discourse-cdn.com/v4/letter/t/76d3ee/32.png) [@tcs](https://techcommunity.softwareag.com/u/tcs)\
**Post date:** [November 26, 2007, 12:07pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/1 "2007-11-26T12:07:18Z")

</div>

Hi,

IP := 2797 SPRINGHILL DR ALBANY, OR, 23456

#TEMP := 2797 SPRINGHILL DR ALBANY  
OP1 := 2797 SPRINGHILL DR  
OP2 := ALBANY

In the samples shown above i want the outputs has shown in in OP1 and OP2.

First step i am using an examine to find ‘,’ and moving that to the #Temp Variable.

From the #Temp variable i want to do an reverse read to find the position of the first space and from that position move that to OP2.

thanks,  
TCS  
😃

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [November 26, 2007, 5:43pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/2 "2007-11-26T17:43:15Z")

</div>

There are several ways to do this;

EXAMINE BACKWARD #TEMP would work

or

MOVE #TEMP (PM=I) TO #TEMP followed by an EXAMINE

or

SEPARATE #TEMP

My guess is that the SEPARATE might be the most efficient, but I have not done a comparison as yet; perhaps later today.

One more possibility, do a SEPARATE on IP and retain the delimiters, then look for the first comma

steve

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [November 26, 2007, 6:51pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/3 "2007-11-26T18:51:12Z")

</div>

Okay, here is a PC timing comparison of two approaches

DEFINE DATA LOCAL  
1 IP (A50) INIT \<‘2797 SPRINGHILL DR ALBANY, OR, 23456’\>  
1 #PIECES (A15/1:20)  
1 OP1 (A50)  
1 OP2 (A50)  
1 #INDEX (I2)  
1 #COMMA (I2)  
1 #BLANK (I2)  
1 #OP2LENGTH (I2)  
1 #LOOP (I4)  
1 #CPU-START (I4)  
1 #CPU-ELAPSED (I4)  
END-DEFINE  
\*  
MOVE _CPU-TIME TO #CPU-START  
SETA. SETTIME  
FOR #LOOP = 1 TO 10000  
SEPARATE IP INTO #PIECES (_) WITH RETAINED DELIMITERS  
EXAMINE #PIECES (\*) FOR ‘,’ GIVING INDEX #INDEX  
MOVE #PIECES (#INDEX - 1) TO OP2  
COMPRESS #PIECES (1:#INDEX - 2) INTO OP1  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE ‘separate ip’ \*TIMD (SETA.) #CPU-ELAPSED

* * *

MOVE \*CPU-TIME TO #CPU-START  
SETB. SETTIME  
FOR #LOOP = 1 TO 10000  
EXAMINE IP FOR ‘,’ GIVING POSITION #COMMA  
EXAMINE DIRECTION BACKWARD SUBSTRING (IP,1,#COMMA) FOR ’ ’ GIVING POSITION #BLANK  
MOVE SUBSTRING (IP,1,#BLANK) TO OP1  
ADD 1 TO #BLANK  
COMPUTE #OP2LENGTH = #COMMA - #BLANK  
MOVE SUBSTRING (IP,#BLANK,#OP2LENGTH) TO OP2  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE ‘examine ip’ \*TIMD (SETB.) #CPU-ELAPSED  
END

Page 1 07-11-26 08:16:44

separate ip 7 66  
examine ip 2 19

On the mainframe I think the second approach will look even better since the SUBSTRING facility there was recently rewritten and is substantially more efficient than on the PC.

steve

---

<div class="post-metadata">

**Author:** ![Weihnachtsbar](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/weihnachtsbar/32/146_2.png) [@Weihnachtsbar](https://techcommunity.softwareag.com/u/Weihnachtsbar)\
**Post date:** [November 26, 2007, 7:02pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/4 "2007-11-26T19:02:31Z")

</div>

Steve Robinson wrote:

> [@](#):
>
> On the mainframe I think the second approach will look even better

But you will get:

```auto
NAT0132 Operand not defined or not of alphanumeric/binary format.
```

during catalog.

Even with the current NAT42x

```auto
EXAMINE DIRECTION BACKWARD
```

is not supported.

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [November 26, 2007, 7:49pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/5 "2007-11-26T19:49:22Z")

</div>

My bad. Difficult to keep track of which options are on which platform.

Here is a rewrite of the second approach:

MOVE \*CPU-TIME TO #CPU-START  
SETC. SETTIME  
FOR #LOOP = 1 TO 10000  
EXAMINE IP FOR ‘,’ GIVING POSITION #COMMA  
MOVE SUBSTRING (IP,1,#COMMA) (PM=I) TO #WORK  
EXAMINE #WORK FOR ’ ’ GIVING POSITION #BLANK  
COMPUTE #BLANK = #BLANK - 2  
MOVE SUBSTRING (#WORK,2,#BLANK) (PM=I) TO OP1  
MOVE SUBSTRING (IP,#BLANK) TO OP2  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE ‘examine no backward ip’ \*TIMD (SETC.) #CPU-ELAPSED

Here are the three times on the PC

```
PAGE # 1 DATE: Nov 26, 2007
PROGRAM: STRIP03 LIBRARY: INSIDE

```

separate ip 7 67  
examine ip 2 19  
examine no backward ip 2 20

So on the PC, the third solution is slightly slower than the second.

On the mainframe, I ran the first and third (second, as noted, will not compile)

separate ip 2 11  
examine no backward ip 1 6

So the ratio is about two to one in favor of the last solution.

steve

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:48pm UTC](https://techcommunity.softwareag.com/t/reverse-read/75746/6 "2021-04-02T14:48:56Z")

</div>


