Announcement

Collapse
No announcement yet.

Iterating nested Lau tables

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

  • Iterating nested Lau tables

    I populate table "all_stations_table" like this:

    this_station_table = {};
    this_station_table.FtpUsername = SessionVar.Expand("%FtpUsername%");
    this_station_table.FtpPassword = SessionVar.Expand("%FtpPassword%");
    this_station_table.DestinationFolderPath = SessionVar.Expand("%DestinationFolderPath%");
    this_station_table.Comment = SessionVar.Expand("%Comment%");
    Table.Insert(all_stations_table, Table.Count(all_stations_table)+1, this_station_table);


    I can successfully retrieve individual stations like this:

    Dialog.Message("First station", all_stations_table[1].FtpUsername..all_stations_table[1].FtpPassword..all_stations_table[1].DestinationFolderPath..all_stations_table[1].Comment);
    Dialog.Message("Second station", all_stations_table[2].FtpUsername..all_stations_table[2].FtpPassword..all_stations_table[2].DestinationFolderPath..all_stations_table[2].Comment);



    I tried to create a function to display each value of each station:

    function DisplayValuesForAllStations()
    for index,value in pairs(all_stations_table) do
    each_station_table = {}; --declare a new table to store each row
    each_station_table[index] = value;
    for thisStationIndex,thisStationValue in pairs(each_station_table) do
    Dialog.Message("Table Item", thisStationIndex .. "=" .. thisStationValue);
    end
    end
    end


    When I call that function, however, I get this error:
    Attempt to call global 'DisplayValuesForAllStations' (a nil value)

    Why would those two Dialog.Message calls (shown above in blue) work - and that function fail?

    Thank you!

  • #2
    My original question above was simply a scope question; please disregard that question. But I'm having a different problem with that same function.
    Click image for larger version

Name:	Screenshot.jpg
Views:	1
Size:	168.3 KB
ID:	284319

    Why would thisStationValue have a table value? I assigned them all string values as can be seen from the fact that the two (commented-out) Dialog.Message lines work fine.

    Please help...
    Thank you!

    Comment


    • #3
      Any help on this (second) post would be greatly appreciated!!!

      Comment


      • #4
        Your code is wrong. This should work much better. I prefer using Hungarian notation for sample projects, as it makes errors easier to spot.

        Code:
        [COLOR="#008000"]-- initialize table[/COLOR]
        [COLOR="#0000FF"]local[/COLOR] tAllStations [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{};[/COLOR]
        
        [COLOR="#008000"]-- populate five elements of tAllStations with tables (tNewStation)[/COLOR]
        [COLOR="#0000FF"]for[/COLOR] i [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]1[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#000000"]5[/COLOR] [COLOR="#0000FF"]do[/COLOR]
          [COLOR="#0000FF"]local[/COLOR] tNewStation [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{};[/COLOR] 
          tNewStation[COLOR="#FF0000"].[/COLOR]FtpUsername [COLOR="#FF0000"]=[/COLOR] [COLOR="#800080"]"FTPUsername"[/COLOR] [COLOR="#FF0000"]..[/COLOR] i[COLOR="#FF0000"];[/COLOR]
          tNewStation[COLOR="#FF0000"].[/COLOR]FtpPassword [COLOR="#FF0000"]=[/COLOR] [COLOR="#800080"]"FTPPassword"[/COLOR] [COLOR="#FF0000"]..[/COLOR] i[COLOR="#FF0000"];[/COLOR]
          tNewStation[COLOR="#FF0000"].[/COLOR]DestinationFolderPath [COLOR="#FF0000"]=[/COLOR] [COLOR="#800080"]"DestinationFolder"[/COLOR] [COLOR="#FF0000"]..[/COLOR] i[COLOR="#FF0000"];[/COLOR]
          tNewStation[COLOR="#FF0000"].[/COLOR]Comment [COLOR="#FF0000"]=[/COLOR] [COLOR="#800080"]"Comment"[/COLOR] [COLOR="#FF0000"]..[/COLOR] i[COLOR="#FF0000"];[/COLOR]
          Table[COLOR="#FF0000"].[/COLOR]Insert(tAllStations[COLOR="#FF0000"],[/COLOR] [COLOR="#FF0000"]#[/COLOR]tAllStations + [COLOR="#000000"]1[/COLOR][COLOR="#FF0000"],[/COLOR] tNewStation)[COLOR="#FF0000"];[/COLOR]
        [COLOR="#0000FF"]end[/COLOR]
        
        [COLOR="#008000"]-- walk the tables and print the data to the Debug window[/COLOR]
        [COLOR="#0000FF"]function[/COLOR] DisplayValuesForAllStations()
          [COLOR="#008000"]-- walk tAllStations (each element is a table)[/COLOR]
          [COLOR="#0000FF"]for[/COLOR] nIndex1[COLOR="#FF0000"],[/COLOR] tCurrentStation [COLOR="#0000FF"]in[/COLOR] [COLOR="#0000FF"]pairs[/COLOR](tAllStations) [COLOR="#0000FF"]do[/COLOR]
            [COLOR="#008000"]-- walk each tCurrentStation (indexed as string, string - associative array)[/COLOR]
            [COLOR="#0000FF"]for[/COLOR] sIndex2[COLOR="#FF0000"],[/COLOR] sValue [COLOR="#0000FF"]in[/COLOR] [COLOR="#0000FF"]pairs[/COLOR](tCurrentStation) [COLOR="#0000FF"]do[/COLOR]
              Debug[COLOR="#FF0000"].[/COLOR]Print([COLOR="#800080"]"tAllStations["[/COLOR] [COLOR="#FF0000"]..[/COLOR] nIndex1 [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"]['"[/COLOR] [COLOR="#FF0000"]..[/COLOR] sIndex2 [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"'] = "[/COLOR] [COLOR="#FF0000"]..[/COLOR] sValue [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"[COLOR="#800080"]\r[/COLOR][COLOR="#800080"]\n[/COLOR]"[/COLOR])[COLOR="#FF0000"];[/COLOR]
            [COLOR="#0000FF"]end[/COLOR]
          [COLOR="#0000FF"]end[/COLOR]
        [COLOR="#0000FF"]end[/COLOR]
        
        Debug[COLOR="#FF0000"].[/COLOR]ShowWindow()[COLOR="#FF0000"];[/COLOR]
        DisplayValuesForAllStations()[COLOR="#FF0000"];[/COLOR]
        This is the output:
        Code:
        tAllStations[1]['Comment'] = Comment1
        tAllStations[1]['FtpUsername'] = FTPUsername1
        tAllStations[1]['FtpPassword'] = FTPPassword1
        tAllStations[1]['DestinationFolderPath'] = DestinationFolder1
        tAllStations[2]['Comment'] = Comment2
        tAllStations[2]['FtpUsername'] = FTPUsername2
        tAllStations[2]['FtpPassword'] = FTPPassword2
        tAllStations[2]['DestinationFolderPath'] = DestinationFolder2
        tAllStations[3]['Comment'] = Comment3
        tAllStations[3]['FtpUsername'] = FTPUsername3
        tAllStations[3]['FtpPassword'] = FTPPassword3
        tAllStations[3]['DestinationFolderPath'] = DestinationFolder3
        tAllStations[4]['Comment'] = Comment4
        tAllStations[4]['FtpUsername'] = FTPUsername4
        tAllStations[4]['FtpPassword'] = FTPPassword4
        tAllStations[4]['DestinationFolderPath'] = DestinationFolder4
        tAllStations[5]['Comment'] = Comment5
        tAllStations[5]['FtpUsername'] = FTPUsername5
        tAllStations[5]['FtpPassword'] = FTPPassword5
        tAllStations[5]['DestinationFolderPath'] = DestinationFolder5
        Ulrich

        Comment

        Working...
        X