ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Shift an array to the left

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

  • #16
    The way that I have it shifts everything to the left automatically when you put data into MyArray - by putting MyArray2 back to originating array it will be shifted one to the left.

    Comment


    • #17
      Code:
      Dcl-C Arrsze const(100);                                   
      Dcl-C Arrsze2 const(99);                                   
      Dcl-s MyArray Char(32) dim(Arrsze);                        
      Dcl-S MyArray2 Char(32) Dim(Arrsze2) based(MyArray2_Ptr);  
      Dcl-s Myarray2_Ptr Pointer Inz(%Addr(MyArray(2)));         
      Dcl-s Orig  Char(32) Dim(100);                             
      Dcl-S i Int(3);                                            
      ...
      For i = 1 to %Elem(MyArray);    
        Orig(i) = %Char(i);           
      Endfor;                         
      MyArray = Orig;                 
      Orig = MyArray2;                
      MyArray(100) = ' ';             
      *INLR = *On;
      I populate Orig. I move Orig to MyArray. Myarray2 is "shifted" so when I move it back to Orig it has 2-100 , the last element needs to be cleared - and the now Orig is shifted one to the left.

      Comment


      • #18
        It would be very simple to create a linked list. The size is dynamic and it won't perform any better than that.

        Comment


        • #19
          Thanks again for all the good ideas, everybody. For now I'm going with J. Taylor's code.

          Code:
          MyArray1 = %subarr(MyArray :2); 
           MyArray = MyArray1;
          I originally had a loop, but I suspect the two eval's will a lot faster than all the stuff going on behind a loop.

          Comment


          • #20
            FWIW, that's is (conceptually, anyway) what memmove() does under the covers, it moves to a temporary area, then moves to the destination -- just like the code that Ted settled on. I guess depending on the implementation, memmove() could potentially run faster (or %subarr might, not sure -- you'd have to benchmark to know) but unless the performance turned out to be a problem, I'd keep the %subarr version since it'll be much easier for the next programmer to understand/maintain.

            I can't imagine the difference would be big.

            Comment


            • #21
              You can also do the following:

              Code:
                     Dcl-DS Array_DS;
                       MyArray              Char(32) Dim(100);
                       MyArray2            Char(32) Dim(100) Pos(33);
                     END-DS Array_DS;
              Fill up MyArray. When you want to shift it left:
              Code:
                     MyArray = MyArray2;
              You only move the data once.

              Comment


              • #22
                Sorry to add in this Post, I am not able to do a new post -
                I am calling a RPG module, which is returning a array as result, now i need to accept that array in my S.so in the INOUT parameters
                i need to code an array


                Can I get an example



                Comment


                • #23
                  Originally posted by jollyegeorge View Post
                  Sorry to add in this Post, I am not able to do a new post -
                  This forum software has some strange behavior regarding new user creating posts, and replies getting flagged as spam.
                  Perhaps make another attempt to create a new post. When it works, you're more likely to be seen by the many helpful people here.

                  Comment


                  • #24
                    I don't know how memmove() works under the covers, whether it creates its own copy or whether it's clever enough to handle the overlap without a temp.

                    Ted, you could do a benchmark to see if memmove() significantly beats using the temp array. If so, I'd use memmove() with some heavy comments to explain what's happening. If it's not significantly better, I'd use the temp array, since as Scott says, it's a bit more obvious what's going on.

                    Ted, I award you a Gold Star for reading the documentation! Well, I couldn't see a gold star emoji, so I'm awarding you a lleaping llama.

                    Comment


                    • #25
                      Thanks, Barbara, but this is an old thread. I don't even remember what I was working on at the time, who I was doing the project for, or why speed was so important. Jolly E. George tacked a new message onto this thread because the forum would not allow him to start a new topic. No one has answered his question, including me. (Haven't had time to look at it.)

                      Life on this planet is full of weird happenings.

                      Comment


                      • #26
                        Oops. I didn't notice the hijack.

                        I'll wait for jollyegeorge's post.

                        Comment


                        • #27
                          Originally posted by jollyegeorge View Post
                          I am calling a RPG module, which is returning a array as result, now i need to accept that array in my S.so in the INOUT parameters
                          i need to code an array
                          Sorry, I don't understand what you are asking. What do you mean by "returning an array as a result"? Do you mean it's passed as a return value? Or as an output parameter?

                          What do you mean by "i need to accept that array in my S"? Looks like there's a word missing there. Perhaps its a stored procedure, since you mention INOUT, and I know that syntax is used in stored procedures. But, I don't know anything more than that... is it an external stored procedure? What language is it written in? How are you calling it?

                          More technical details would be very helpful here. If possible, post an example of the code.

                          Comment

                          Working...
                          X