3 Replies Latest reply on Mar 15, 2012 4:40 PM by philmodjunk

    Repetition - count of

    dzittin

      Title

      Repetition - count of

      Post

      Simple Q, but I cannot find the answer. If I create a repetition, say:

      $a[1] = "x"

      $a[2] = "y"

      $a[3] = "z"

       

      How can I tell how many repetitions are in the list. I know (it's 3 :-) this example is simple, but I want to create a loop structure that processes each of the a[i] values and this list can grow (or shrink) by adding a value to the end (last+1). I can manually change the exit condition on the loop, but this is not as robust from a maintenance standpoint. Yes, I realize I can also use a list (ValueCount, GetValue), but the code isn't as readable as I would like, especially when the lists are long.

       

      It would be nice if FM had a Unix-like

      foreach $str in "x" "y" "z"

      do

      somethingto($str)

      done

      construct (I put this in the suggestion box). I seem to need this loop on a list functionality fairly frequently and can only think of value lists or repetitions to manage such a loop. If anyone has a better idea, I am all ears. In any event, I would like to know how to determine the valid number of elements in a repetition.

      As usual, TIA for the good info flow on this Forum.

      -D

        • 1. Re: Repetition - count of
          philmodjunk

          How do you load the repeating variable with values in the first place? Might you increment a second variable each time a value is assigned and use it to keep track of the number of repetitions?

          • 2. Re: Repetition - count of
            dzittin

            Hi PMJ,

            I do a brain dead:

            set variable[$a[1], "str1"]

            set variable[$a[2], "str2"]

            set variable[$a[3], "str3"]

            ...

            I thought of incrementing an index value, but this is messy, although arguably more robust.

            Pseudo code:

            set variable[$i, 1]

            set variable[$a[$i], "str1"]

            set variable[$i, $i+1]

            set variable[$a[$i], "str2"]

            ....

            The later is arguably the way to get the robustness I want, it just means a lot of code when the list is > 10 or so items.

            Thanks,

            -D

             

            • 3. Re: Repetition - count of
              philmodjunk

              With this sample code, why wouldn't you just use:

              Set variable [$I ; 3 ]