ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Array and data structure processing.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array and data structure processing.

    Hi everyone,

    I'm having a moment and wondered if someone could point me the right way.

    I've got a screen which will allow me to create adjustments to an order, these (once confirmed) will eventually post into a file, but I don't want to post them to a file until the users confirms.

    So I though I could create an array with a data structure mapped over it to hold me field level data.

    Code:
    d adjArray        ds                  qualified dim(1000)
    d item                                like(Item#)
    d OrgQty                              like(OrdQty)
    d NewQty                              like(OrdQty)
    d Reference                           like(OrdRef)
    So I plan to build this array with data entered from the screen, if a user tries to enter something for an item already entered I want to retrieve the values from my array / data structure.

    My issue is retrieving from my array based only on item number?

    Will this work, if so how do I go about checking for existence of my item, if not, what is the best way of going about it?

  • #2
    Re: Array and data structure processing.

    I've gotten around my issue by looping through all elements in my array and looking where adjArray(x).Item = dspItem, although I can't help but think that there must be a better way to do this?

    Comment


    • #3
      Re: Array and data structure processing.

      I've been working on a program with a similar requirement recently. I wrote my own lookup routine for the array.

      I have a variable that keeps up with how many elements have been loaded. Each time, I add an element to the array, I increment that variable. So my loop starts with the last element loaded and searches backwards thru the array. I end the loop when I find a match or the loop control variable gets to zero, whichever comes first.

      I don't have the RPG code at hand, but the pseudocode would be something like this:

      Code:
      loop-control = Number of elements that have been loaded
      
      dow loop-control > zero 
      and array.item [loop-control] <> SearchItem
         loop-control = loop-control - 1
      end dow
      
      // if loop-control is zero, the search item is not in the array
      It may not be the best solution, but it's working and I'm not touching it.

      Comment


      • #4
        Re: Array and data structure processing.

        Thanks Ted,

        That's almost exactly what I've done, in the example below "x" holds the total loaded elements in my array..

        Code:
        y= 1;
        dow y <= x;
          if adjArray(y).item = dspItem;
            // do processing here;
          endif;
          y += 1;
        enddo;

        Comment


        • #5
          Re: Array and data structure processing.

          Unless I'm misunderstanding the manual and/or the requirement, wouldn't this work:

          Code:
          index = %lookup(dspItem:adjArray(*).item:1:x);
          If index > 0;
          // array entry found
          Else;
          // array entry not found
          Endif;

          Comment


          • #6
            Re: Array and data structure processing.

            hi Brian,

            I did try that originally and get compile errors "*RNF0571 The second parameter for %LOOKUPxx is not valid."

            *edit* should point out I'm on V5R4

            Comment


            • #7
              Re: Array and data structure processing.

              The ability to search like Brian showed was added in V7R1

              Comment


              • #8
                Re: Array and data structure processing.

                I guess it depends on how big you think the array might get. If it's going to get huge (and I'm guessing that it isn't) there might be a more efficient way of arranging the structure, but for only a handful of entries it would be overkill to do anything other than a linear search.

                Comment


                • #9
                  Re: Array and data structure processing.

                  Thanks Scott and Ray,

                  I've only got 1,000 elements in my array, you're right I don't think they will notice the extra processing time needed, I'd just hoped for a cleaner coding solution, which as Scott says is available at a later release

                  Comment


                  • #10
                    Re: Array and data structure processing.

                    Originally posted by ChrisL
                    I'd just hoped for a cleaner coding solution, which as Scott says is available at a later release
                    Tell me about it - we are stuck on V5R4 ... and are very unlikely to move to anything else.

                    Comment


                    • #11
                      Re: Array and data structure processing.

                      V5R4, gotcha. How about this, then:

                      Code:
                      d adjDs           ds                  
                      d  adjArray                    100    dim(1000)
                      d   item                              like(Item#) overlay(adjArray)
                      d   OrgQty                            like(OrdQty) overlay(adjArray:*next)
                      d   NewQty                            like(OrdQty) overlay(adjArray:*next)
                      d   Reference                         like(OrdRef) overlay(adjArray:*next)
                      
                      index = %lookup(dspItem:item:1:x);
                      If index > 0;
                      // array entry found
                      Else;
                      // array entry not found
                      Endif;
                      Note: I just guessed at the length of adjArray as 100, replace it with the actual length. Also, I took out the QUALIFIED keyword as I'm not sure it will work with this technique, but it might.

                      Comment


                      • #12
                        Re: Array and data structure processing.

                        Te %LOOKUP (like Brian's first post) should work if you're on 7.1

                        For earlier releases, a simple loop shoudl work fine.
                        Code:
                          for x = 1 to %elem(adjArray);
                            if adjArray(x).item = ThingToSearchFor;
                                found = x;
                                leave;
                            endif;
                        endfor;
                        I can't see why you'd convert this to an overlay array, which is extremely ugly and cumbersome to deal with. A loop just isn't that hard to write.

                        Comment


                        • #13
                          Re: Array and data structure processing.

                          Try something like this.

                          d @item S dim(%elem(adjArray)) like(Item#)

                          @item = adjArray;
                          if %lookup(dspItem:@item) >*zero;

                          Comment

                          Working...
                          X