10 Replies Latest reply on Apr 22, 2016 11:06 AM by marty72

    Combining 2 Variables in a Script

    marty72

      I have a loop in a script where I am trying to combine the values of two variables but I am having trouble with the syntax (see attached or below).

      I have one set of variables called $spc1, $spc2, $spc3, etc. set to a text values ("PW", "PR", etc.)

      I have another variable called $counter set to a numeric value that is incremented inside of a loop (1, 2, etc.).

      I am trying to increment the number value in the first variable by using the $counter variable in the loop.

      Inside of the loop I am trying to set a 3rd variable called $spcloop to be a combination of $spc1 and $counter so that $spcloop becomes PW, PR, etc as it moves through the loops.

      I tried the following without success:

      Set Variable [$spcloop: Value: $&"spc"&"$counter"]

      Set Variable [$spcloop: Value: $&"spc"&$counter]

      Set Variable [$spcloop: Value: $&"spc$counter"]

      Any assistance would be appreciated.

      Thanx.

      Marty

       

      Combining Variable in Script.png

        • 1. Re: Combining 2 Variables in a Script
          rgordon

          This would be a good case to use a repeating variable instead of the the 5 different Set Variables in the beginning of your script.  Then you could use $spc [$counter] in your loop

          • 2. Re: Combining 2 Variables in a Script
            brucewilson

            Set Variable [ $spcloop ; Evaluate ( "$spc" & $counter ) ]

            1 of 1 people found this helpful
            • 3. Re: Combining 2 Variables in a Script
              keywords

              The following is a much simpler version of your script:

               

              Set Variable [ $spc; Value:"PW¶PR¶PJ¶SW¶SB" ] Set Variable [ $partTwo; Value:"_BA_POLE" ] Set Variable [ $partThree; Value:"_SUM" ]
              Set Variable [ $counter; Value:0 ]

              Loop
              Set Variable
              [ $counter; Value:$counter + 1 ]

              Set Variable [ ]
              Exit Loop If [ $counter = 5 ]

              Set Field [  yourFeildName ; GetValue ( $spc ; $counter ) & $partTwo ] End Loop

               

              • 4. Re: Combining 2 Variables in a Script
                marty72

                Thank you for the assistance.

                I tried the following by adding some temporary test fields. All calc'd correctly except for the last numeric calculation. There is just a ? in there so the calculation is not working correctly. It is going to a related table for the Summary field (which does have a value).


                Set Variable [ $spc; Value:"PW¶PR¶PJ¶SW¶SB" ]

                Set Variable [ $part1; Value:"MAIN_SPC_BA::" ]

                Set Variable [ $part2; Value:"_BA_POLE" ]

                Set Variable [ $part3; Value:"TREE_SPC_BA::" ]

                Set Variable [ $part4; Value:"_BA_POLE_SUM" ]
                Set Variable [ $counter; Value:0 ]

                Loop
                Set Variable
                [ $counter; Value:$counter + 1 ]

                Set Variable [ ]
                Exit Loop If [ $counter = 5 ]

                Set Field [  MAIN_SPCLOOP_VALUE ; GetValue ( $spc ; $counter ) ]      Calcs field to correct value from $spc

                Set Field [  MAIN_SPCLOOP_VALUE2 ; $part1 & GetValue ( $spc ; $counter ) & part2 ]      Provides proper field ref. as MAIN_SPC_BA:SB_BA_POLE

                Set Field [  MAIN_SPCLOOP_VALUE3 ; $part3 & GetValue ( $spc ; $counter ) & $part4 ]      Provides proper field ref. as TREE_SPC_BA:SB_BA_POLE_SUM

                Set Field [  MAIN_SPCLOOP_VALUE_NUM ; ( GetFieldName $part3 & GetValue ( $spc ; $counter ) & $part4 ) * 2 ]      This however does not calc a value - should take value from TREE_SPC_BA:SB_BA_POLE_SUM and multiply by 2 - instead has ? in field.

                End Loop


                • 5. Re: Combining 2 Variables in a Script
                  thurmes

                  At some point in this script you need to use the Evaluate function. I can't quite tell from your script where you expect the contents of the field to actually show up, and where you want the fields to be set to the name of a field.

                   

                  For instance, field MAIN_SPCLOOP_VALUE3, on the first trip through the loop, will be set to the text TREE_SPC_BA::PW_BA_POLE_SUM, not the contents of TREE_SPC_BA::PW_BA_POLE_SUM.

                  Similarly, MAIN_SPCLOOP_VALUE_NUM will be set to some text, not to a number than can be multiplied.


                  The solution is simple:

                  Set Field [  MAIN_SPCLOOP_VALUE_NUM ; Evaluate ( $part3 & GetValue ( $spc ; $counter ) & $part4 ) * 2 ]

                  Use of the Evaluate function elsewhere in the loop will also put the contents of the named fields into the target fields, if that's what you wish to do.

                  • 6. Re: Combining 2 Variables in a Script
                    marty72

                    The solution is simple:

                    Set Field [  MAIN_SPCLOOP_VALUE_NUM ; Evaluate ( $part3 & GetValue ( $spc ; $counter ) & $part4 ) * 2 ]

                    Use of the Evaluate function elsewhere in the loop will also put the contents of the named fields into the target fields, if that's what you wish to do.


                    Thank you. This produced  the correct result in this temp field. Can I replace MAIN_SPCLOOP_VALUE_NUM with an Evaluate as well? Or would I need to use GetFieldAsName or Set Field by Name?

                    I am trying to put the results of the calculaton above TREE_SPC_BA::PW_BA_POLE_SUM * 2 into a related table with a field called  MAIN_SPC_BA::PW_BA_POLE.


                    • 7. Re: Combining 2 Variables in a Script
                      thurmes

                      The easiest answer is "try it and see". Again, I couldn't quite tell what you were trying to do in the script, whether the end-all of what you were doing was to try to get the contents of one set of fields into another set of fields (I think that's what you're doing), or whether in the end you'll want some of those fields populated with the names of the fields.

                       

                      There are a couple problems that I see with your script:

                      1) it looks like you should exit loop if $count > 5, rather than =5, or you'll miss the last item in $spc

                      2) it looks like you should use Evaluate for each of the last three Set Field commands in your loop; not sure what the first one is accomplishing

                      3) it looks like you'll keep changing the contents of those fields as you go through the loop; perhaps a way to accomplish what you want would be to have these fields have five repetitions, and

                      Set Field [  MAIN_SPCLOOP_VALUE_NUM[$count] ; Evaluate ( $part3 & GetValue ( $spc ; $counter ) & $part4 ) * 2 ]; this way, each loop will populate a different repetition of the fields.

                      1 of 1 people found this helpful
                      • 8. Re: Combining 2 Variables in a Script
                        marty72

                        1 - yes makes sense

                        2 - yes makes sense

                        3 - not sure

                        For each of the 5 tree species (5 species for testing only - will be many more later) I need to go through the loop once and take the value from a field (in a related child table) called TREE_SPC_BA::PW_BA_POLE_SUM, do a calculation with the data in this field, and put the result of the calculation in a a similarly named field called MAIN_SPC_BA::PW_BA_POLE. I need the loop to change the tree species portion of the field name each time through the loop.

                        ...._BA::PW_BA_POLE_SUM

                        ...._BA::PR_BA_POLE_SUM

                        ...._BA::SB_BA_POLE_SUM, etc.

                        Your evaluate suggestion is getting me the correct result from the TREE_SPC_BA table fields and will put it into my temporary testing field. But I can't get it to populate the corresponding field in the MAIN_SPC_BA table fields. The 2 character tree species reference in this field needs to change in the loop as well. I tried the following so far:


                        Set Field By Name Evaluate ( $part1 & GetValue ( $spc ; $counter ) & $part2) * 2 ; Evaluate ( $part3 & GetValue ( $spc ; $counter ) & $part4 ) * 2 ]

                        Trying to do the following within the loop:

                        Set FieldMAIN_SPC:_BA::PW_BA_POLE ; TREE_SPC_BA::PW_BA_POLE_SUM * 2 ]

                        Set FieldMAIN_SPC:_BA::PR_BA_POLE ; TREE_SPC_BA::PR_BA_POLE_SUM * 2 ]

                        • 9. Re: Combining 2 Variables in a Script
                          erolst

                          An approach that may save you headaches to just keep synchronized, ready-made lists of source and target field references; using

                           

                          Set Variable [ $sourceList ;

                          List ( GetFieldName ( SourceTable::field1 ) ; GetFieldName ( SourceTable::field1 ) { ; etc. } )

                          ]

                           

                          Set Variable [ $targetList ;

                          List ( GetFieldName ( TargetTable::field1 ) ; GetFieldName ( TargetTable::field1 ) { ; etc. } )

                          ]

                           

                          GetFieldName() instead of hardcoded strings allows you to rename the fields or tables without breaking the references.

                           

                          Then in your loop declare/process the references as


                          Set Variable [ $curSource ; GetValue ( $sourceList ; $loopCounter ) ]

                          Set variable [ $curValue ; GetField ( $curSource ) ]

                          Set Variable [ $curResult ; calculation using $curValue ]

                          Set Variable [ $curTarget ; GetValue ( $targetList ; $loopCounter ) ]


                          and simply do:


                          Set Field by Name [ $curTarget ; $curResult ]


                          If you object that your potential source and target field lists are much too long, and that you need to calculate the references from component lists – then that probably is evidence that your data model is flawed / not properly normalized – too many similar fields.


                          Processing many records with one (relevant) field is programmatically much easier (and maintainable) than looping through many fields of a single record.

                          1 of 1 people found this helpful
                          • 10. Re: Combining 2 Variables in a Script
                            marty72

                            Thank you, that solved it for me. !

                            1 of 1 people found this helpful