6 Replies Latest reply: May 12, 2014 9:23 AM by Saad Tadili RSS

    $ sign expansion

    Saad Tadili

      Hi,

       

      In wonder why when I use $sign expansion in the condition of a do while instruction it doesn't work :

       

      The following code :

       

      LET vCompteur = 1;

       

      DO while $(vCompteur) <= 10

          TRACE $(vCompteur);

          LET vCompteur = $(vCompteur) + 1;

      LOOP

       

      results in an infinite loop

       

       

      while the following code :

       

      LET vCompteur = 1;

       

      DO while vCompteur <= 10

          TRACE $(vCompteur);

          LET vCompteur = $(vCompteur) + 1;

      LOOP

       

      prints in the script execution console (as expected)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

       

      I have to say i'm kind of lost here, I tought $ sign expansion will return the value of vCompteur.

       

      thanks

        • Re: $ sign expansion
          Alessandro Saccone

          It works in this way because $ expansion computer again the expression and assign the value of 1 (as it was a constant) while the new variable value is stored in vCompteur

            • Re: $ sign expansion
              Saad Tadili

              Hi,

               

              I just don't seem to understand.

               

              At the first loop, $ expansion will compute the expression and will find that vCompteur is equal 1. But in the loop the value of the variable vCompteur will change to 2 and then when $ expansion will compute the expression normally it should find that vCompteur is equal 2.

               

              Something doesn't make sense, or maybe I'm missing something.

               

              Thanks

                • Re: $ sign expansion
                  Jonathan Dienst

                  Hi

                   

                  Actually your second script is actually the correct way to code this loop. Used this way vCompteur behaves like a variable in a normal language, and so does the do while loop

                   

                  To explain why the first one does not work - which is that the variable expansion happens before the script line is interpreted, this creates a compare between two number literals, not a variable and a number. The while condition is set up from this (once), hence an infinite loop.

                   

                  HTH

                  Jonathan

              • Re: $ sign expansion
                Vadim Tsushko

                Welcome to fun world of QlikView scripting

                 

                I believe not all assumptions from common stock programming languages are working here.

                To illustrate my point I slightly modified your script. It is still perfectly valid, and give correct output, though syntax highlighter not happy with lines 6-7 now.

                 

                ///$tab Main
                LET vCompteur = 1;
                LET whileVar = 'vCompteur';
                LET doWhile = 'DO while';
                LET whileCondition = '<= 10';
                $(doWhile) $(whileVar) $(whileCondition)
                    TRACE $(vCompteur);
                    LET vCompteur = $(vCompteur) + 1;
                    LET whileVar = 'ABRAKADABRA';
                LOOP
                LET vCompteur=;
                LET whileVar=;
                LET doWhile=;
                LET whileCondition=;
                

                 

                 

                Best way to look at inner working of qlikview script engine is to step through script debugger.

                Lets step through up to 6 line.

                Debugger - Waiting 2014-05-12 16.44.27.png

                 

                Before debugger step into command line each occurence of $(something) in that line is textually replaced by its value in key/value map in right-bottom box (dictionary/map for variables).

                If we step through futher we can see that debugger never return to line 6. Loop doing its job between lines 7-10.

                So our intentional distortion of variable whileVar do not break script either.


                Firstly I intuitively think about $(someThing) as of evaluation of variable someThing. It's actually purely a string substitution (regex replace) on a stage before any evaluation.


                I hope it would clarify this vague behaviour.