1 2 Previous Next 15 Replies Latest reply on Apr 21, 2016 5:03 AM by erolst

    Creating list variable (array) and using them?

    Stigge

      Hi.. its me again =)

       

      Now i got another issue i seem not to be able to solve.

       

      Tables:

      - Software

      - Logs

       

      Software Fields:

      ID, HostName, Software, Installed ver, License, Record ID

       

      Log Fields:

      ID, HostName, Status, Field info, Start text, New Text, Date, Account

       

      My task in a script is to get the following matcher per record i have in to Logs:

       

      Logs --> Record 1

      ID = Software::ID

      HostName = Software::HostName

      Status = "Logged"

      Field info = Software::Record ID & " - " & Software::Software

      Start Text = Software::ID    <--- Record 1 collects the first field from record 1 in Software

      New Text = Software::ID    <--- Record 1 collects the first field from record 1 in Software

       

      Logs ---> Record 2

      ID = Software::ID

      HostName = Software::HostName

      Status = "Logged"

      Field info = Software::Record ID & " - " & Software::Software

      Start Text = Software::Software    <--- Record 2 collects the second field from record 1 in Software

      New Text = Software::Software    <--- Record 2 collects the second field from record 1 in Software

       

      etc ....

       

      Am stuck at trying to create the script for this.. :/

      I would like to use List($MyList) but i dont know how to set the values in the list and then get them out to the fields.. :/

      Anyone who can teach and old dog this ? =D been googling now for like hours :/

        • 1. Re: Creating list variable (array) and using them?
          User26869

          As you said, you might use $list = List ( field1 ; field2 ; field3 ; etc) to get the value of each field on a record. To extract field1 from $list use GetValue( $list ; 1). You can put a counter instead of the number in GetValue.

           

          Chloe

          • 2. Re: Creating list variable (array) and using them?
            Stigge

            Hey Chloe..

             

            Is it something like this ?

             

             

            this is beeing bad to me aswell..

             

            But still after running the script i get this:

             

             

             

            It still not going to next record and collecting in to the list() ... :/

            • 3. Re: Creating list variable (array) and using them?
              erolst

              Some generic scripting; note that the crucial part in compiling the list is to use the variable name itself in the declaration:

               

              IN:

               

              Loop

                Set Variable [ $myList ; List ( $myList ; ThisTable::aField ) ]

                Go to Next R/R [ exit after last ]

              End Loop

               

              OUT:

               

              Set Variable [ $theCount ; ValueCount ( $myList ) ]

              Loop

              Exit Loop if [ Let ( $count = $count + 1 ; $count > $theCount ) ]

              Set Field [ AnotherTable::anotherField ; GetValue ( $myList ; $i ) ]

              End Loop

               

              Note that depending on the number of fields to transfer, it might be easier to


              A. just collect a primary key, create a new record, set the foreign key and use a number of Replace Field Contents via a relationship, or


              B. make sure the relevant (source) records are the found set, change to the target table, import the source records (matching the relevant fields) and set any necessary keys batch-wise into that imported set.

              1 of 1 people found this helpful
              • 4. Re: Creating list variable (array) and using them?
                User26869

                As erolst pointed out, you need to loop to build the list, but then you go a list of all the values of one field across all records.

                If you want the value of several fields for one record, just:

                 

                Set Variable ($R ; Value: List ( Software::ID ; Software::Software ; ...) )

                 

                as you did, but without the $R in the value of the list (as you're not iterating on it, you want a different value for each record).

                 

                Correct me if I'm wrong

                 

                Chloe

                • 5. Re: Creating list variable (array) and using them?
                  Stigge

                  I get what you guys are thinking.

                  But i dont get how to be able to use it in my script. to get what i want with it..

                  In this case i have one field that collects different fields from same record.

                  1 Software record  = 4 Logs records.

                  • 6. Re: Creating list variable (array) and using them?
                    Stigge

                    So if i get the looping correct..

                     

                    in Software in this case i have 3 records that i need to create log records from..

                    so i need to create the list with all fields per records directly by looping..

                    So list($R; 1)  = record 1, field 1

                    And List($R;6) = record 2, field 1

                    And List($R;15) = record 3, Last field

                     

                    ?? have i understood correctly then ?

                    • 7. Re: Creating list variable (array) and using them?
                      User26869

                      In Software, for each record, you want Set Variable [ $R ; Value = List ( Software::ID ; Software::Software ; ...) ], so that each time you are in a new Software record, you get a "new" $R variable with the information from the record only.

                       

                      So that you always now in your second loop which element of the list corresponds to which field of the Software record you were on.

                      To expand:

                       

                      Go to first record

                      Loop

                       

                      #you are in Software, first record

                      #Get the list of fields you want

                      Set Variable [ $R ; Value = List ( Software::ID ; Software::Software ; ) ]

                       

                      #Now go to the log

                      Go to layout [Logs]

                      New record

                      Set field [ LOG::ID ; GetValue($R; 1) ]

                      Set field [ LOG::Field ; GetValue($R ; number corresponding to the field you want to log in Software) ]

                      Set field .... until all the fields you need on the log are filled

                       

                      #Back to Software to go to next record

                      go to layout

                      go to next record

                      End loop

                       

                       

                      I haven't put the inner loop to simplify and also because I don't understand what you want to do with it, as most of the information is the same for all records?

                       

                      Chloe

                      • 8. Re: Creating list variable (array) and using them?
                        erolst

                        It's easier if you don't perform the layout change within the loop; that could be costly. Here's a variant that uses variable repetitions to read and write all required data in one go:

                         

                        # [ read ]

                        Go to Record/Request [ first ]

                        Loop

                          Set Variable [ $keyList  ; List ( $keyList ; Software::id ) ]

                          Set Variable [ $dataList [ repetition: Software::id ] ; List ( Software::source1 ; Software::source2 … ) ]

                          # [ one data list per record, with the primary ID as key ]

                          Go to Record/Request [ next ; exit after last ]

                        End Loop

                        # [ write ]

                        Go to Layout ( Log [ Log ] )

                        Loop

                          Exit Loop if [ Let ( $i = $i + 1 ; $i > ValueCount ( $keyList ) ]

                          Set Variable [ $currentKey ; GetValue ( $keyList ; $i ) ]

                          Set Variable [ $currentDataList ; Evaluate ( "$keyList[" & $currentKey & "]" )

                          New Record

                          Set Field [ Log::id_software ; $currentKey ]

                          Set Field [ Log::target1 ; GetValue ( $currentDataList ; 1 ) ]

                          Set Field [ Log::target2 ; GetValue ( $currentDataList ; 2 ) ]

                          Set Field [ Log::target4 ; GetValue ( $currentDataList ; 3 ) ]

                          # [ which could also be put into a loop, using Set Field by Name and a list of target field names)

                        End Loop

                        Go to Layout [ original ]


                        This is merely as a demo, since I also

                        User26869 wrote:

                        don't understand what you want to do with it, as most of the information is the same for all records?

                        What exactly does that Log table log?

                        • 9. Re: Creating list variable (array) and using them?
                          User26869

                          Yes, better to collect the data first and then write it on the records you want. The script is much easier to read and understand that way as well

                          • 10. Re: Creating list variable (array) and using them?
                            Stigge

                            Hi.. going to give that one a try =) thank you..

                             

                            The log script now collects the starting values of all(most of them) fields in the records.

                            What i have done before is on the Computer i have logged all its fields, to have a starting point upon changes. Så i can easy track changes.

                            Collecting Start text: Field A = "Apa"

                            Collecting Start text: Field B = "Elefant"

                            Collect a Change: A = "Apa" --> "Gorilla"

                             

                            As i said, Computer part is already done. And now each computer have number of Software related to it, so i need to track them aswell and their editable fields. So the script now collects the "Start Text", so i can compare upon changes.

                            PC 1 might have 3 Software attached.

                            • 11. Re: Creating list variable (array) and using them?
                              Stigge

                              Tested you code erolst.

                              Didnt go so good.

                              here is the code:

                              Edited: found a error on raw 34.. corrected..

                              Software:

                               

                              Outcome:

                              edited: new outcome after correcting code on raw 34...

                               

                              only posted 1 record of one of the fields. i might have done some error in the code, not sure. checking it now.

                              did a Dialog to check what values it has.. and that wasnt that good :/

                              • 12. Re: Creating list variable (array) and using them?
                                erolst

                                I'm afraid that the repetition seems to be always converted to a numerical value – which is either a bug or a feature. So that key isn't really a key, but an index.

                                 

                                So if you work with a numerical ID, you should be OK – with text keys you could use GetAsNumber() when reading the repetition, but I think that then uniqueness would no longer be guaranteed.

                                 

                                OTOH, why not just use a relationship: compile the keys, create a loop, set the Log foreign key value, read the values from the related fields … etc.

                                 

                                PS: FileMaker doesn't use an index position of 0; all indices are 1-based (except – for whatever reason – the Choose() function.) Probably has to do with appealing to a wider audience.

                                • 13. Re: Creating list variable (array) and using them?
                                  Stigge

                                  i just solved it..

                                  Probl not the best written code, but it does the job as i needed it to..

                                   

                                  Resault:

                                  • 14. Re: Creating list variable (array) and using them?
                                    wimdecorte

                                    erolst wrote:

                                     

                                    I'm afraid that the repetition seems to be always converted to a numerical value – which is either a bug or a feature. So that key isn't really a key, but an index.

                                     

                                    So if you work with a numerical ID, you should be OK – with text keys you could use GetAsNumber()

                                     

                                    A better approach is you want to work with "named" repetitions is to use Code() around the text string.  This will make sure the number generated by the Code() call is always unique to the string; which may not be true

                                     

                                    So basically:

                                    $my_var[ Code( "Wim" )]

                                     

                                    This is the concept of "named buckets" that recently came up in a another thread; I'm preparing a blog post about it.

                                    1 2 Previous Next