[JW Lua] Iteration of Tables

Jan Angermüller jan at angermueller.com
Sun Nov 30 22:14:39 CET 2014


Great, that you found a solution !
A simple indexed table without explicit index keys and then using ipairs 
might
have been easier to implement and easier to read. Something like:

page_texts = {
   {["Rawstring"]="^font(Font0)^size(42)^nfx(0)^title()",
    ["PageAssignment"]=0,
    ["FirstPage"]=1,
    ["LastPage"]=1,
    ["HorizontalAlignment"]=finale.TEXTHORIZALIGN_CENTER,
    ["VerticalAlignment"]=finale.TEXTVERTALIGN_TOP,
    ["PageEdgeRef"]=true,
    ["HorizontalPos"]=-0,
    ["VerticalPos"]=-72,
    ["FrameEdgeRef"]=true,
    ["IndependentRightPage"]=false,
    ["HorizontalAlignmentRightPage"]=finale.TEXTHORIZALIGN_CENTER,
    ["HorizontalPosRightPage"]=0,
    ["VerticalPosRightPage"]=-72,
    ["Visible"]=true,
   },
   {["Rawstring"]="^font(Font0)^size(18)^nfx(1)^subtitle()",
    ["PageAssignment"]=0,
    ["FirstPage"]=1,
    ["LastPage"]=1,
    ["HorizontalAlignment"]=finale.TEXTHORIZALIGN_CENTER,
    ["VerticalAlignment"]=finale.TEXTVERTALIGN_TOP,
    ["PageEdgeRef"]=true,
    ["HorizontalPos"]=-0,
    ["VerticalPos"]=-72,
    ["FrameEdgeRef"]=true,
    ["IndependentRightPage"]=false,
    ["HorizontalAlignmentRightPage"]=finale.TEXTHORIZALIGN_CENTER,
    ["HorizontalPosRightPage"]=0,
    ["VerticalPosRightPage"]=-72,
    ["Visible"]=true, }
   }

for i,v in ipairs(page_texts) do
   print(i,v["Rawstring"])
end

Best regards,
Jan



Am 30.11.2014 21:43, schrieb Bruce Olson:
> Hi Jan,
>
> Yes, I saw all the same conundrum in researching this so far. I could just
> change the order of the table or use
> Pairsbykeys, but eventually I want to serialize the table using file io, so
> I need the order NOT to be dependent on the rawstring, but rather on the
> index. Guess I need to think about how to add an IndexKey to the structure
> of the table.
>
> I wonder if page_texts = {
> [1] = {["^font(Font0)^size(42)^nfx(0)^title()"] = {
>     ["PageAssignment"]=0,
>     ["FirstPage"]=1,
>     ["LastPage"]=1,
>     ["HorizontalAlignment"]=finale.TEXTHORIZALIGN_CENTER,
>     ["VerticalAlignment"]=finale.TEXTVERTALIGN_TOP,
>     ["PageEdgeRef"]=true,
>     ["HorizontalPos"]=-0,
>     ["VerticalPos"]=-72,
>     ["FrameEdgeRef"]=true,
>     ["IndependentRightPage"]=false,
>     ["HorizontalAlignmentRightPage"]=finale.TEXTHORIZALIGN_CENTER,
>     ["HorizontalPosRightPage"]=0,
>     ["VerticalPosRightPage"]=-72,
>     ["Visible"]=true,
> },
> }
> }
>
> will work?
>
> More woodshedding ahead I see.
>
> -- 
> Regards,
> Bruce
>
> From: JWLua [mailto:jwlua-bounces at jwmusic.nu] On Behalf Of Jan Angermüller
> Sent: Sunday, November 30, 2014 2:19 PM
> To: The JW Lua script plug-in.
> Subject: Re: [JW Lua] Iteration of Tables
>
> Hi Bruce,
>
> if you look at the pairs(table) description in
> http://lua-users.org/wiki/ForTutorial :
> "Note that the order that items are returned is not defined, not even for
> indexed tables."
>
> If you use ipairs instead, the order in which elements are returned is
> guaranteed
> to be in the numeric order of the indices.
>
> If you use pairsbykeys, they will be sorted in alphabetical key order.
> That's not the order that you built up the table in your code
> (i.e. your keys are not sorted in alphabetical order). Maybe that's a hint ?
>
> Best regards,
> Jan
>
>
> Am 30.11.2014 20:51, schrieb Bruce Olson:
> Hi Jan,
>   
> Yes, I had already looked at both those places. There are no useful
> parameters to sort on, so sorting the table is not helpful. It is actually
> already sorted. It’s that the pairs iterator doesn’t use that order for some
> reason. And there is not another iteration scheme from the Lua docs that
> seems to help.
>   
> -- 
> Regards,
> Bruce
>   
> From: JWLua [mailto:jwlua-bounces at jwmusic.nu] On Behalf Of Jan Angermüller
> Sent: Sunday, November 30, 2014 1:37 PM
> To: The JW Lua script plug-in.
> Subject: Re: [JW Lua] Iteration of Tables
>   
> Hi Bruce,
>
> I haven't read your code, but have you checked Jari's
> explanation for sorting and parsing Lua tables:
> http://jwmusic.nu/jwplugins/wiki/doku.php?id=jwlua:development#coll2table
>
> You can also have a look at the Lua manual:
> http://www.lua.org/pil/7.3.html
>
> Maybe this helps ?
>
> Best regards,
> Jan
>
> Am 30.11.2014 20:24, schrieb Finale at web.YOSound.com:
> Hi Jari,
>   
> How do I iterate through a table so that the order does not change. In the
> attached code using “for k, v in pairs()” I get a different order each time
> it is run. If I use “for k, v in pairsbykeys()” it is always in the same
> order, just not the order I want. ;) I’d like to get it in the order of the
> table. Thanks for all your work and your prompt replies.
>   
> Best regards,
> Bruce
>   


-- 
Jan Angermüller
Jevenstedter Str. 80
22547 Hamburg
Tel. 040 - 28 94 84 82
www.angermueller.com





More information about the JWLua mailing list