ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

create table with D spec

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

  • create table with D spec

    I want to use operation 'lookup' for lookup TAB1 and TAB2. I will create the table at the bottom of source as sample below.
    **
    A Apple
    B Banana
    C Cat
    D Dog

    How I need to code the table in D Spec????

  • #2
    Re: create table with D spec

    try

    D TabCode S 1 Dim(4) CtData Ascend PerRcd(1)
    D TabName S 14 Dim(4) Alt(TabCode)
    Hunting down the future ms. Ex DeadManWalks. *certain restrictions apply

    Comment


    • #3
      Re: create table with D spec

      Tables are generally considered "old hat" and are only present in RPG IV for compatibility reasons. A simple array is far more flexible and useful.

      If the array/table is of any significant size, you should also consider using %LOOKUP (or &TLOOKUP if you insist on using tables) rather than the old LOOKUP op-code.

      LOOKUP is much much much slower, requires the use of (shudder) numbered indicators and is an old function.

      Comment


      • #4
        Re: create table with D spec

        Jon,

        Would you please post some good ole code snippets?

        Thank you sir
        jamie
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: create table with D spec

          look up example using arays.

          Code:
          FoundIndex =  %LOOKUP(ThisCode:ArayCode:01);
          If FoundIndex = 0;
              // Not Found
          Else;
              // Found,  do someting               
              ThisAnimal = ArayName(FoundIndex);
          EndIf;
          Hunting down the future ms. Ex DeadManWalks. *certain restrictions apply

          Comment


          • #6
            Re: create table with D spec

            Originally posted by jamief
            Jon,

            Would you please post some good ole code snippets?

            Thank you sir
            jamie
            Avec plaisir monsieur!

            I decided to show a double array rather than a simple one so there's a bit more to this but I hope it explains itself. Code is not tested but verifies clean with WDSC.

            Code:
                 
                 d                 ds
                 d myArray                             dim(9999) Ascend
                 d   field1                       4a   overlay(myArray)
                 d   field2                       4a   overlay(myArray: *next)
                 
                  // count is set during array load to reflect number of active entries
                 d count           s              5i 0
                 d searchFor       s              4a
                 d index           s              5i 0
                 
                 
                  /Free
                   // sort and then search only loaded portion of array 
                   sorta %subarr( field1: 1: count);
                   index = %Lookup( searchFor: field1: 1: count);  
                   if index > 0; // We found it!
                     dsply ('Found "' + searchFor + '" at index ' + %Char(index));
                     dsply ('Value of associated field2 is ' + field2(index));
                   else;
                     dsply ('"' + searchFor + '" not found');
                   endif;
                   // To sort and search on field2 simply replace "field1" by 
                   //   "field2" in the above code.  Note that once the array 
                   //   is sequenced on field2 a %Lookup on field1 is no longer valid 
            
                   *inLR = *On;
            Last edited by JonBoy; March 21, 2006, 09:50 AM.

            Comment


            • #7
              Re: create table with D spec

              I tried to get that example in fixed font using courier - but it ignores me - sorry - hope the example is still clear.

              Comment


              • #8
                Re: create table with D spec

                Just place the [ code] [ /code] (remove the spaces)

                around your code to format it.

                [ php] [ /php] works nicely but php doesnt like slashes so its out for FREE


                jamie
                All my answers were extracted from the "Big Dummy's Guide to the As400"
                and I take no responsibility for any of them.

                www.code400.com

                Comment


                • #9
                  Re: create table with D spec

                  Originally posted by jamief
                  Just place the [ code] [ /code] (remove the spaces)

                  around your code to format it.

                  jamie
                  Thanks Jamie - that looks much better.

                  Comment


                  • #10
                    Re: create table with D spec

                    Your welcome......Glad to have you back lurking about.


                    take care
                    jamie
                    All my answers were extracted from the "Big Dummy's Guide to the As400"
                    and I take no responsibility for any of them.

                    www.code400.com

                    Comment

                    Working...
                    X