# X-Array Code Not Working

**URL:** <https://techcommunity.softwareag.com/t/x-array-code-not-working/99599>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [January 27, 2012, 6:31pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599 "2012-01-27T18:31:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan4](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@Dan4](https://techcommunity.softwareag.com/u/Dan4)\
**Post date:** [January 27, 2012, 6:31pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/1 "2012-01-27T18:31:09Z")

</div>

I’d like to use a dynamic array in my code and found some documentation about X-Arrays. My version of Natural is 4.2.6, so I believe this should work. When I run my program, it doesn’t blow up. However, it doesn’t show me any output for my dynamic array. Here’s my code (with file and key names changed to generic names):

In my local data area, I have my array defined as:

01 #ITEM-ARRAY(A8/1:_) /_ DYNAMIC ARRAY W/UNLIMITED UPPER BOUND

```auto

READ WORK FILE 4 #WKF04
  ADD 1 TO #WF4-READ
  MOVE #X TO #KEY-X
  MOVE #ITEM TO #KEY-ITEM
  FD-IT. FIND [FILE] WITH [KEY] = #KEY
    IF *NUMBER(FD-IT.) = 0
      ADD 1 TO #NOFOUND
      DISPLAY '=' #NOFOUND /* JUST TO SEE IF MY CODE GETS THIS FAR
      EXPAND ARRAY #ITEM-ARRAY TO (1:#NOFOUND) /*EXPAND X-ARRAY
      MOVE #ITEM TO #ITEM-ARRAY(#NOFOUND)
      ESCAPE BOTTOM
    END-IF
    ADD 1 TO #FOUND
    .... continue processing for found records
  END-FIND
END-WORK
*
WRITE 'RESULTS OF PROGRAM:' /
  #WF4-READ (EM=ZZZ,ZZZ,ZZZ,ZZ9) 'WORK FILE 4 RECS READ' /
  #FOUND(EM=ZZZ,ZZZ,ZZZ,ZZ9) 'RECS FOUND'
  IF #NOFOUND > 0
    WRITE #NOFOUND(EM=ZZZ,ZZZ,ZZZ,ZZ9) 'RECS NOT FOUND'
    FOR #I = 1 TO #NOFOUND
      WRITE / 21T #ITEM-ARRAY(#I)
    END-FOR
  END-IF
END
```

Here is my output:

## PAGE 1 #NOFOUND

RESULTS OF PROGRAM:  
134 WORK FILE 4 RECS READ  
0 RECS FOUND

I’ve manipulated my work file data so that all 134 records should not be found. Any ideas of where I may be going wrong?

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [January 27, 2012, 6:57pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/2 "2012-01-27T18:57:48Z")

</div>

If your FIND at line 5 does not find anything, it will not enter the loop, so the IF \*NUMBER at line 6 never gets executed. You need to move the IF \*NUMBER below the find loop.

---

<div class="post-metadata">

**Author:** ![Dan4](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@Dan4](https://techcommunity.softwareag.com/u/Dan4)\
**Post date:** [January 27, 2012, 7:19pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/3 "2012-01-27T19:19:00Z")

</div>

Thanks, Jerome!

---

<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:** [January 28, 2012, 11:51am UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/4 "2012-01-28T11:51:26Z")

</div>

You might also want to take a look at the IF NO RECORDS FOUND clause of the FIND statement.

---

<div class="post-metadata">

**Author:** ![Dan4](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@Dan4](https://techcommunity.softwareag.com/u/Dan4)\
**Post date:** [January 28, 2012, 3:04pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/5 "2012-01-28T15:04:50Z")

</div>

Thanks for your response, Steve. I was told to steer clear of the IF NO RECORDS FOUND clause because it initiates another processing loop whereas the IF \*NUMBER = 0 avoids that. Is that not true or is the difference between the two negligible?

---

<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:** [January 28, 2012, 4:35pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/6 "2012-01-28T16:35:39Z")

</div>

No, it doesn’t really initiate a processing loop, what the documentation says [here](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat427mf/sm/find.htm#IF_NO_RECORDS_FOUND-clause) is

> [@](#):
>
> If no records meet the specified WITH and WHERE criteria, the IF NO RECORDS FOUND clause causes the FIND processing loop to be executed once with an “empty” record.

---

<div class="post-metadata">

**Author:** ![Dan4](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@Dan4](https://techcommunity.softwareag.com/u/Dan4)\
**Post date:** [January 28, 2012, 6:15pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/7 "2012-01-28T18:15:19Z")

</div>

Thank you, Wolfgang. This shows me that I need to do my homework and read the Software AG documentation to verify any future “helpful programming tips” that I read out there online.

---

<div class="post-metadata">

**Author:** ![Douglas\_Kelly](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/douglas_kelly/32/102_2.png) [@Douglas\_Kelly](https://techcommunity.softwareag.com/u/Douglas_Kelly)\
**Post date:** [January 30, 2012, 3:42am UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/8 "2012-01-30T03:42:23Z")

</div>

Just a few other notes: the EXPAND (or RESIZE) command is a relatively expensive one to use - typical expansion requires allocating new space, copying old values in and freeing up old space.

Whenever you know what size your array will be, use that rather than incrementing the array size one element at a time. In some cases, \*NUMBER contains the number of records that will be returned, so you can use an AT START OF DATA or IF \*COUNTER = 1 and EXPAND the array to hold \*NUMBER occurrences. If your loop will contain conditional logic such that your array will likely end up with fewer than \*NUMBER occurrences filled in, use the REDUCE statement outside the loop to reduce the dynamic array down to its actual occurrences. In your example, you might start with an estimate of how many items will be read and match your criterion for adding to the array, and if you fill that up, increase the array by some factor (25%? 50%? 100 occurrences? 1000 occurrences? know your data!) with an EXPAND before adding the next row of data rather than doing an EXPAND on every iteration through the loop.

Other application scenarios may have other logic needed, but the goal is to minimize the number of times you have to execute the EXPAND statement to help minimize CPU usage.

---

<div class="post-metadata">

**Author:** ![Finn\_the\_Dane1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/finn_the_dane1/32/132_2.png) [@Finn\_the\_Dane1](https://techcommunity.softwareag.com/u/Finn_the_Dane1)\
**Post date:** [January 30, 2012, 12:47pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/9 "2012-01-30T12:47:01Z")

</div>

I think the problem is that if there are no hits to the FIND statements, then you will never enter the find-loop, and thus never test the \*number condition.  
The simple trick is to replace the test with a  
IF NO [RECORDS] [FOUND]  
…statement  
END-NOREC  
(see:[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat822mf/sm/find.htm#IF\_NO\_RECORDS\_FOUND-clause](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat822mf/sm/find.htm#IF_NO_RECORDS_FOUND-clause))

In this way you will force the program execution into the source part where you do the x-array magic 😉

Finn

---

<div class="post-metadata">

**Author:** ![Dan4](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@Dan4](https://techcommunity.softwareag.com/u/Dan4)\
**Post date:** [January 30, 2012, 3:18pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/10 "2012-01-30T15:18:32Z")

</div>

> [@Douglas Kelly](#):
>
> In your example, you might start with an estimate of how many items will be read and match your criterion for adding to the array, and if you fill that up, increase the array by some factor (25%? 50%? 100 occurrences? 1000 occurrences? know your data!) with an EXPAND before adding the next row of data rather than doing an EXPAND on every iteration through the loop.

Thanks for the good suggestion, Douglas. If I understand you correctly, I could initialize my upper bound of the array at 100, and then if that upper bound is reached, increase it by a certain percentage or number. So my code would then look like this:

```auto

READ WORK FILE 4 #WKF04
  ADD 1 TO #WF4-READ  
  MOVE #X TO #KEY-X  
  MOVE #ITEM TO #KEY-ITEM  
  FD-IT. FIND [FILE] WITH [KEY] = #KEY  
    IF NO RECORDS FOUND
      ADD 1 TO #NOFOUND  
      IF #NOFOUND = 1
        EXPAND ARRAY #ITEM-ARRAY TO (1:#U-BOUND) /*INITIAL EXPANSION OF X-ARRAY (INITIALIZED AT 100 IN LDA)
      END-IF
      MOVE #ITEM TO #ITEM-ARRAY(#NOFOUND)  
      IF #NOFOUND = #U-BOUND
        COMPUTE #U-BOUND = #U-BOUND + 25
        EXPAND ARRAY #ITEM-ARRAY TO (1:#U-BOUND) /*INCREASE UPPER BOUND OF X-ARRAY
      END-IF
      ESCAPE BOTTOM  
    END-NOREC  
    ADD 1 TO #FOUND  
    .... continue processing for found records  
  END-FIND  
END-WORK
```

---

<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, 3:01pm UTC](https://techcommunity.softwareag.com/t/x-array-code-not-working/99599/11 "2021-04-02T15:01:30Z")

</div>


