ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Clear vs *Blanks

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

  • Clear vs *Blanks

    I have a very weird behavior going on and I cannot understand it. I am pulling down some json and processing it and it randomly fails for trailing garbage but the json coming from the service is correct.

    What I found is it appears that when I clear my variable it isn't, don't' really understand it. Have to use debug and monitor the memory in order to see it but once I changed from clear to *Blanks everything works.

    All code in a loop to grab orders until there is no more.


    Code:
    [FONT=Calibri][FONT=Consolas][COLOR=red]clear[/COLOR][/FONT][FONT=Consolas][COLOR=black] returnData[/COLOR][/FONT][FONT=Consolas][COLOR=purple]; // fails with extra } at the end from the previous request[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=black]returnData [/COLOR][/FONT][FONT=Consolas][COLOR=purple]=[/COLOR][/FONT] [FONT=Consolas][COLOR=olive]*Blanks[/COLOR][/FONT][FONT=Consolas][COLOR=purple]; // Works just fine[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Consolas][COLOR=black]rc [/COLOR][/FONT][FONT=Consolas][COLOR=purple]=[/COLOR][/FONT][FONT=Consolas][COLOR=black] http_url_get_raw[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=black][URL="url:fd:%25Paddr(incoming):300"]url:fd:%Paddr(incoming):300[/URL][/COLOR][/FONT][FONT=Consolas][COLOR=purple]);[/COLOR][/FONT][/FONT]
    
    docNode =
    yajl_buf_load_tree( %addr( returnData )+2
    :%Size(returnData)
    :errMsg);
    
    [FONT=Calibri][FONT=Consolas][COLOR=teal]//************************************************** *****************************************[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=teal]//Incoming - process incoming http request data[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=teal]//************************************************** *****************************************[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=purple]Dcl-Proc[/COLOR][/FONT][FONT=Consolas][COLOR=black] Incoming[/COLOR][/FONT][FONT=Consolas][COLOR=purple];[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=red]Dcl-Pi[/COLOR][/FONT][FONT=Consolas][COLOR=black] *n [/COLOR][/FONT][FONT=Consolas][COLOR=maroon]int[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=blue]10[/COLOR][/FONT][FONT=Consolas][COLOR=purple]);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=black]fd [/COLOR][/FONT][FONT=Consolas][COLOR=maroon]int[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=blue]10[/COLOR][/FONT][FONT=Consolas][COLOR=purple])[/COLOR][/FONT] [FONT=Consolas][COLOR=maroon]value[/COLOR][/FONT][FONT=Consolas][COLOR=purple];[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=black]data [/COLOR][/FONT][FONT=Consolas][COLOR=maroon]char[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=blue]65535[/COLOR][/FONT][FONT=Consolas][COLOR=purple])[/COLOR][/FONT] [FONT=Consolas][COLOR=maroon]options[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=olive]*varsize[/COLOR][/FONT][FONT=Consolas][COLOR=purple]);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=black]len [/COLOR][/FONT][FONT=Consolas][COLOR=maroon]int[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=blue]10[/COLOR][/FONT][FONT=Consolas][COLOR=purple])[/COLOR][/FONT] [FONT=Consolas][COLOR=maroon]value[/COLOR][/FONT][FONT=Consolas][COLOR=purple];[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Consolas][COLOR=red]End-Pi[/COLOR][/FONT][FONT=Consolas][COLOR=purple];[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Consolas][COLOR=black]http_xlate[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=black]len[/COLOR][/FONT][FONT=Consolas][COLOR=purple]:[/COLOR][/FONT][FONT=Consolas][COLOR=black]data[/COLOR][/FONT][FONT=Consolas][COLOR=purple]:[/COLOR][/FONT][FONT=Consolas][COLOR=black]to_ebcdic[/COLOR][/FONT][FONT=Consolas][COLOR=purple]);[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Consolas][COLOR=black]returnData [/COLOR][/FONT][FONT=Consolas][COLOR=purple]=[/COLOR][/FONT] [FONT=Consolas][COLOR=blue]%Trim[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=black]returnData[/COLOR][/FONT][FONT=Consolas][COLOR=purple])[/COLOR][/FONT] [FONT=Consolas][COLOR=purple]+[/COLOR][/FONT] [FONT=Consolas][COLOR=blue]%Subst[/COLOR][/FONT][FONT=Consolas][COLOR=purple]([/COLOR][/FONT][FONT=Consolas][COLOR=black]data[/COLOR][/FONT][FONT=Consolas][COLOR=purple]:[/COLOR][/FONT][FONT=Consolas][COLOR=blue]1[/COLOR][/FONT][FONT=Consolas][COLOR=purple]:[/COLOR][/FONT][FONT=Consolas][COLOR=black]len[/COLOR][/FONT][FONT=Consolas][COLOR=purple]);[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Consolas][COLOR=purple]Return[/COLOR][/FONT][FONT=Consolas][COLOR=black] len[/COLOR][/FONT][FONT=Consolas][COLOR=purple];[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Consolas][COLOR=purple]End-Proc;[/COLOR][/FONT][/FONT]

  • #2
    Well there is needed information missing from what you posted.

    For example how is returnData defined?

    Also why are you using http_url-get-raw? It is rather unusual - http_string() or http_req are far more commonly used.

    CLEAR will return a variable to its default initialization. If that was indeed blanks then the two statements should have the same effect.

    Comment


    • #3
      We can't see how returnData is defined.
      If it is fixed length then clear and eval *blanks works the same way.

      If returnData is defined as a varying length field then eval *blanks will fill it with blanks in its entire length
      BUT clear will set it length to zero !!!
      The data part will not be changed.
      This means that when in debug you type eval to see it you will see the old data as well.
      Type eval returnData:x to see it in hex format and you will see the length part as well -- it is the first 2 or 4 bytes.

      This means that you should change your program to ( assuming that returnData is a varying length field)

      a)
      clear returnData;
      // remove this returnData = *Blanks

      b)
      in procedure incoming

      // remove this returnData = %Trim(returnData) + %Subst(data:1:len);
      returnData += %Subst(data:1:len);

      Comment


      • #4
        It is a varchar.

        http_url-get-raw is just something I copied at some point and it seemed to work so I didn't dig deeper.

        I tried updating the statement and it did not work. But after that explanation it makes sense, I need to use %Len instead of %Size in the load tree procedure.

        Also I tried reset and that also is successful while using %Size.

        Thanks for the help in understanding.

        Code:
        Dcl-S returnData varchar(65000);
        
        docNode =
        yajl_buf_load_tree( %addr( returnData )+2
        :%len(returnData)
        :errMsg);
        
        returnData += %Subst(data:1:len);

        Comment


        • #5
          And one more tip.
          In the yajl_buf_load_tree() statement you are using the old technique
          %addr( returnData )+2

          This will work as long as returnData is not longer than 65535 characters. Then 2 bytes are used for storing the length.
          But if you exceed this then 4 bytes are used for storing the length.

          So in order to get the address of the data part of the variable length field you should use this instead:
          %addr( returnData : *DATA )

          Then you don't have to worry if 2 or 4 bytes are used. And it gives you a more robust code.

          Comment

          Working...
          X