ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

MoveL

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

  • MoveL

    Say I have a character field 10 A, I want to put a '42' in front, so I would do this
    MoveL '42' Field

    In free form Field = '42', but clears everything but the 42. I want to keep what is in the field and change the first two characters only. Sorry new to RPG

  • #2
    Code:
    %subst(Field: 1: 2) = '42'

    Comment


    • #3
      As an alternative:
      Code:
      d myExample       ds
      d   myFld                       10a
      d     myPrefix                   2    overlay( myFld )
      
          myPrefix = '42' ;
      By declaring as a structure, sub-fields can be declared in various ways. Multiple definitions can be declared over the same area.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4

        Ted's solution is the easiest, simplest. Another way:
        Code:
        Field = '42' + %Subst(Field: 3) ;

        Comment


        • #5
          I've always lived by the theory that if a piece of code performs as expected... then even though it is not exactly how I would write it... if it ain't broke, then don't "fix" it. All the answers given here will work for the question asked.

          Because the OP is a self-appointed new person to RPGLE, I would suggest a vote for Tom's answer, as it provides a more complete solution to the posted question. That is, where Tom's code could be copied and pasted into any RPGLE program and it will work. (remembering of course, silly errors like "duplicate field names")... Just my nickle's worth there Ringer. (I bow with respect in your general direction).

          Of course, my opinion all comes down to style and personal preferences. Which... is kind of outside the scope of the original question here... so now, we return you to our regularly scheduled answers.


          Best Regards.

          Fred Williams

          Comment


          • #6
            Originally posted by Whitecat27 View Post
            I've always lived by the theory that if a piece of code performs as expected... then even though it is not exactly how I would write it... if it ain't broke, then don't "fix" it. All the answers given here will work for the question asked.

            Because the OP is a self-appointed new person to RPGLE, I would suggest a vote for Tom's answer, as it provides a more complete solution to the posted question. That is, where Tom's code could be copied and pasted into any RPGLE program and it will work. (remembering of course, silly errors like "duplicate field names")... Just my nickle's worth there Ringer. (I bow with respect in your general direction).

            Of course, my opinion all comes down to style and personal preferences. Which... is kind of outside the scope of the original question here... so now, we return you to our regularly scheduled answers.


            Best Regards.

            Fred Williams
            Why do you like the DS option better? (just curious)

            Comment


            • Whitecat27
              Whitecat27 commented
              Editing a comment
              JTaylor, I'd guess that way back in the days of dinosaurs, the only way there was in S/34 RPG to split up a field into sub-fields was to use a data structure. So, that's the way of thinking about this type of problem/solution I'm most accustomed to internally visualizing... even when I use things like %subst() or %scan(), my brain is imagining a DS.

              I chose Tom's DS answer because it offered a discrete and detailed answer to the OP question, who appeared to be less experienced than other people here, and also because in the case of this example, it required a trivial solution for a possible "one time only" problem.

              To me... the overall advantage to Tom's solution is that it gives a newer RPG programmer the concept that calc specs work on things that are defined as being in memory, therefore, there's a definition area, and a calculation area in the example itself. The example can be simply cut and pasted from this forum page, and allow the programmer to get on with their life.

              Barbara makes an excellent point below. I absolutely agree that the "DS Solution" should have a qualified data structure. Her example further refines the principle of code being self-documenting, and also keeps intact the discrete differences in where the data comes from, and where the data is changed or referred to.


              Best Regards,

              Fred Williams

          • #7
            I don't understand why there'd be a preference for Tom's solution. Yeah, it works... but so do the others, and Tom's requires the most code and is the trickiest one to understand. (Since the DS defiinition and calcs are not in the same place, you have to know about code in two places to understand it. Someone reading the calcs will not know a DS is in use without further investigation. Not a big deal, but why do it when there are shorter, simpler ways?)

            Ted's reply, the first one in the thread, shows an extraordinarily simple solution where the code is all in one place, and therefore a no-brainer to read.

            Comment


            • #8
              It's always a matter of taste - I prefer Tom's usually because when you move a value in myFld the field myPrefix is automatically set - no need to do any substring calculations or reassignments... but as always it's one of those things it depends on what the goal is....

              Comment


              • #9
                Originally posted by Scott Klement View Post
                (Since the DS defiinition and calcs are not in the same place, you have to know about code in two places to understand it. Someone reading the calcs will not know a DS is in use without further investigation. )
                For sure if I were to use the DS method, I would use a qualified DS to make it clear that a DS is involved. Even though having a DS instead of a string is more code, sometimes it's good to give names to the various parts of the string, assuming the various parts are always in the same positions.

                Code:
                %subst(Field : 1 : 2) = '45';
                vs

                Code:
                dcl-ds Field qualified;
                    all char(10);
                       whatever char(2) overlay(all : 1);
                       something_else char(8) overlay(all : 3); // or *NEXT, I can't decide which is better for this
                end-ds;
                
                ...
                
                Field.all = something;
                %subst(Field.whatever) = '45';
                Depending on what those 2 bytes mean, it might be easier in the long run to use the more self-documenting code with the DS than to use %subst. Using a named constant for '45' would also be a good idea.

                Comment


                • #10
                  With Barbara's written examples, and our current RPG compilers being able to well-optimize program source code... is this more a discussion of what the source looks like, rather than the eventual machine code generated?




                  Best Regards,

                  Fred Williams

                  Comment


                  • #11
                    To all you fine folks who think complicated is better, I say take a look at this:

                    Code:
                           ctl-opt actgrp(*new) option(*srcstmt:*nodebugio);
                    
                           dcl-s  Field      char(10);
                    
                           *inlr = *on;
                           Field = 'ABCDEFGHIJ';
                           Movel42 (%addr(Field));
                           return;
                    
                           dcl-proc  Movel42;
                              dcl-pi *n;
                                 pData        pointer      value;
                              end-pi;
                    
                              dcl-s   P       pointer;
                    
                              dcl-ds  Data    len(1) based(P);
                                 OneChar      char(1)    pos(1);
                              end-ds;
                    
                              dcl-c   LoVal   x'00';
                              dcl-c   ZoneF   x'F0';
                              dcl-c   Digit4  x'04';
                              dcl-c   Digit2  x'02';
                    
                              dcl-s   LoopControl      uns(3);
                    
                              P = pData;
                    
                              for LoopControl = 1 to 2;
                                 OneChar = %bitand (OneChar: LoVal );
                                 OneChar = %bitor  (OneChar: ZoneF );
                                 if LoopControl = 1;
                                    OneChar = %bitor  (OneChar: Digit4);
                                 else;
                                    OneChar = %bitor  (OneChar: Digit2);
                                 endif;
                                 P += 1;
                              endfor;
                    
                           end-proc;
                    I believe I've one-upped you. Not only do I have a data structure, but also:
                    • pointers and pointer arithmetic
                    • a subprocedure
                    • a loop
                    • an if/else/endif
                    • bit-twiddling.

                    Let's see you top that!

                    (Don't get mad at me, guys. I'm just having a little fun and bringing a little levity to your Friday the 13th! )

                    Comment


                    • #12
                      I could top that ;-)

                      Your code only works in character sets where the '42' is x'F4' and x'F2'. I could easily make this even nastier by writing code to make it work in all character sets!

                      Comment


                      • #13
                        Originally posted by Whitecat27 View Post
                        With Barbara's written examples, and our current RPG compilers being able to well-optimize program source code... is this more a discussion of what the source looks like, rather than the eventual machine code generated?




                        Best Regards,

                        Fred Williams
                        not at all - it has to do with the next guy in to maintain the code - Ted's is the best solution, in this regards - A most courteous approach is to get to the point of what you are trying to do - be able to read the code at face-value without having to jump to another part of the program to understand something - eliminate D specs whenever possible.

                        Comment


                        • #14
                          Originally posted by jayvaughn View Post
                          Ted's is the best solution, in this regards
                          I hope you mean my first solution, Fred, not my second one!

                          Comment

                          Working...
                          X