ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Building strings with apostrophes in the middle

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

  • Building strings with apostrophes in the middle

    Hi,

    I'm attempting to build a command string for cmd = 'SNDBRKMSG TEXT('hello person') TOMSGQ(xxxxxx)'

    It's a no go.. So how do i build a string with apostrophes in the middle?

    thanks
    www.midlifegamers.co.uk

  • #2
    Re: Building strings with apostrophes in the middle

    Escape them with another apostrophe, like

    cmd = 'SNDBRKMSG TEXT(''hello person'') TOMSGQ(xxxxxx)'

    Comment


    • #3
      Re: Building strings with apostrophes in the middle

      Hi Huddy:

      I prefer this method:

      Code:
           d qt              s              1a   inz('''')  
      
                        cmd = 'SNDBRKMSG TEXT(' + qt + 'hello person' + qt + ') TOMSGQ(xxxxxx)'
      Best of Luck
      GLS
      The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

      Comment


      • #4
        Re: Building strings with apostrophes in the middle

        Originally posted by Huddy
        So how do i build a string with apostrophes in the middle?
        In general, you simply build the string with the apostrophe in whatever position it needs to be. It's often easiest to have a 1-byte variable or constant that has an apostrophe as its value, then reference it when building the full string.

        However, the specific method might depend on exactly what problem you're having. For example, there is a difference between (a) building a string from substrings where one of the substrings needs to have apostrophes around it and (b) building a string from substrings where one of the substrings needs to have apostrophes around it and also has one or more embedded apostrophes.
        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


        • #5
          Re: Building strings with apostrophes in the middle

          Many thanks for your kind replies as always. Scott's fix worked a treat and i've noted the other replies.. Not sure how this has escaped me in the past tbh.

          Thanks once again.
          www.midlifegamers.co.uk

          Comment


          • #6
            Re: Building strings with apostrophes in the middle

            Just one other thing.. I get a program dump if i call SNDUSRMSG using QCMDEXC..

            Any reason for this?

            Code:
              @cmd = 'SNDUSRMSG MSG('test') MSGTYPE(*INFO) TOMSGQ(*SYSOPR)';    
                                                                              
               callp    qcmdexc(@cmd:%size(@cmd));
            The dump is a little unhelpful.

            thanks
            Last edited by kitvb1; January 13, 2015, 05:03 AM. Reason: fixed /code tag
            www.midlifegamers.co.uk

            Comment


            • #7
              Re: Building strings with apostrophes in the middle

              Were there any messages in the job log prior to the dump?

              Is @cmd defined as varying? If not you probably want to trim it in the call:

              callp qcmdexc(%trim(@cmd):%size(%trim(@cmd)));

              Comment


              • #8
                Re: Building strings with apostrophes in the middle

                thanks for the reply.

                I didn't think to check my log


                Code:
                                         Additional Message Information                     
                                                                                            
                 Message ID . . . . . . :   CPD0031       Severity . . . . . . . :   30     
                 Message type . . . . . :   Diagnostic                                      
                 Date sent  . . . . . . :   12/01/15      Time sent  . . . . . . :   15:12:5
                                                                                            
                 Message . . . . :   Command SNDUSRMSG not allowed in this setting.         
                 Cause . . . . . :   The complete list of settings includes: batch,         
                   interactive, batch control language (CL) program, interactive CL program,
                   batch REXX procedure, interactive REXX procedure, batch CL ILE program,  
                   interactive CL ILE program, CALL to QCMDEXC, and various types of source 
                   statements. The Display Command (DSPCMD) command can be used to determine
                   the setting in which the command is allowed.                             
                 Recovery  . . . :   Omit the command.
                Any ideas?
                www.midlifegamers.co.uk

                Comment


                • #9
                  Re: Building strings with apostrophes in the middle

                  SNDUSRMSG is not allowed from an interactive session (DSPCMD does not list *INTERACT as a "Where allowed to run").

                  Cheers,

                  Emmanuel

                  Comment


                  • #10
                    Re: Building strings with apostrophes in the middle

                    Looking at SNDUSRMSG, it has to be run from a CL program. Create a generic CL Program and call it from the RPG program.

                    Comment


                    • #11
                      Re: Building strings with apostrophes in the middle

                      Ok, that confirms what i was thinking..

                      Thanks ScottM
                      www.midlifegamers.co.uk

                      Comment


                      • #12
                        Re: Building strings with apostrophes in the middle

                        If you must use SNDUSRMSG, create a CLLE *MODULE and make it part of your program. Call it as a procedure instead of calling QCMDEXC as a procedure. If you don't need SNDUSRMSG but only want similar behavior, and you don't want to have a CLLE *MODULE, then you need to call one of the Send Message APIs. Pick the one that gives the behavior that you need and call it as a procedure instead of calling QCMDEXC. Most developers probably call the QMHSNDPM API.

                        If you need to run a significant command like SNDUSRMSG, I'm not sure why you'd use QCMDEXC instead of creating a CLLE *MODULE. CL makes all of the capabilities available for handling variables and errors, and it does it with compiled commands instead of ones that are parsed/interpreted at run-time. You don't need to create the code to do all of the extra stuff that might be needed. You could, of course, put a lot of that extra code into other procedures (or ignore it as many do), perhaps in *MODULEs and perhaps a *SRVPGM; but then you're back into the position of maintaining a separate object that merely does some of what CL already does for you.

                        (Also, consider learning the Process Commands (QCAPCMD) API and leaving QCMDEXC behind. Much can be learned about command execution by mastering the newer API.)
                        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


                        • #13
                          Re: Building strings with apostrophes in the middle

                          Originally posted by GLS400 View Post
                          Hi Huddy:

                          I prefer this method:

                          Code:
                               d qt              s              1a   inz('''')  
                          
                                            cmd = 'SNDBRKMSG TEXT(' + qt + 'hello person' + qt + ') TOMSGQ(xxxxxx)'
                          Best of Luck
                          GLS
                          I agree with this one except I've always defined it this way.

                          d quote c const(x'7D') single quote


                          Which begs the question, how do I get code in the box that I see in posts?

                          Comment


                          • #14
                            Re: Building strings with apostrophes in the middle

                            Originally posted by corvair61_ View Post
                            I agree with this one except I've always defined it this way.

                            d quote c const(x'7D') single quote
                            IMHO, using const('''') is superior. You are hard-coding the hex value for a quote in the particular CCSID you are using. What if this code gets copied to a system with a different CCSID where a quote is not x'7D'? Or gets converted to something like VARPG that runs on an ASCII system, so quotes are a completely different hex value? Also, the next guy to read the code has to know what x'7D' is in order to understand your code... whereas when you do INZ('''') it's obvious what character it is.

                            This way, the code is easier to understand, and it'll work in any environment.


                            Which begs the question, how do I get code in the box that I see in posts?
                            Type [code] on the line before the code you want to put in the box, and type [/code] on the line afterwards. Everything between [code] and [/code] will go into the box.

                            Comment


                            • #15
                              Re: Building strings with apostrophes in the middle

                              I don't like X'7D' for the same reasons that Scott mentions.

                              But wait, there's more.

                              The issue with X'7D' not necessarily being a quote is now a possibility in 7.2 RPG. If you have a field defined with CCSID(*UTF8), or with some ASCII CCSID, then x'7D' is not a quote when it's used in conjunction with those fields. So if you do say

                              msg = utf8_fld1 + x'7d' + utf8_fld2;

                              then the x'7d' will be interpreted as UTF-8 x'7d' which is not a quote.

                              Code:
                                      dcl-s utf8_fld1 char(5) inz('hello') ccsid(*utf8);
                                      dcl-s utf8_fld2 char(5) inz('world') ccsid(*utf8);
                                      dcl-s msg char(15);                               
                                      dcl-c QT x'7d';                                   
                                      dcl-c QT2 '''';                                   
                                                                                        
                                      msg = utf8_fld1 + QT + utf8_fld2;                 
                                      dsply msg;                                        
                                      msg = utf8_fld1 + QT2 + utf8_fld2;                
                                      dsply msg;                                        
                                      return;
                              Displays
                              Code:
                              DSPLY  hello}world
                              DSPLY  hello'world

                              Comment

                              Working...
                              X