[JW Lua] Some Lua power

Charles O. Lawrence charlesolawrence at bellsouth.net
Sun Sep 1 03:01:48 CEST 2013


Jari,
Thanks for the info on tables.  I had not studied Lua enough yet to know
that this construct was used.  I am familiar with it from my experience with
Perl and JavaScript.

Other members,
A heads up for anybody else having trouble with this.  Maybe it's just me!
When I started to study the example script provided, at first I could not
see the forest for the trees.  I now realize that the note entries (e) that
are being looped through using the iterator are not just a single note.
They could be, but they also can be a chord of any number of notes, or a
rest.  One needs to study the Speedy Edit Frame to get a handle on how it
all relates.  I was mistakenly thinking I should see a count of note heads
in the selected range.

BTW, there should have been a RETURN after the comment line "-- Create an
empty table".

To make it a little easier to see what is going on, think of the table in
the traditional way of a one dimensional array with "n" number or rows (n is
unknown at the start and is to be determined on the fly, a concept not
usually allowed in most "older" languages) and 1 column, where a row
represents a staff, and the value in the table for that row is the count of
note entries on that staff.  When you start the loop, the number of rows
(selected staves) in the table is unknown, so when you encounter a value in
the table for a row (staff number) that does not exist yet, then you "add" a
new row to the table by setting its value (note entry count) to one.  From
now on that row will exist and its note entry count can be incremented by
one.  By the end of the loop you have built and populated the table.  For
example, if your selection contained 3 staves, you would end up with a table
that looks something like this:

 t |   1
---------
1 | 10
2 | 20
3 | 30

Staff 1 had 10 entries, staff 2 had 20, and staff 3 had 30.  It is important
to realize that the rows (staff numbers in this case) do not have to be in
any order, like 1,2,3, or even consecutive, or even numbers for that matter,
or even the same type of data.  For example, they could be a mix of numbers
and text.  The values do not have to be numbers either.  Text is just fine.
This is where the Lua table differs from the traditional array.  In Lua the
staff numbers are the "keys" and the note entry counts are the "values".
They always come in a pair (key, value).  So you can also look at the Lua
table (in this example) as a two dimensional traditional table (3 rows, 2
columns) like this:

t   |  1 (keys)    | 2  (values)
------------------------------
1  |       1             |      10
2  |       2             |      20
3  |       3             |      30

Remember that these arrays doesn't really exist, but are for illustration
only.

It is really like this:

t["key"] = "value"

 Also remember the "keys" and "values" don't have to be numbers.

Lua provides an iterator to loop through the key and value pairs of a table,
as shown.

You other members may not have needed this explanation, but it did me good
to think it through to knock off some of the rust that had accumulated.  If
I got anything wrong, let me know.

Jari,
The Manager won't let you edit or delete an "item in group" (bottom pane) if
you select another "plug-in group" in the top pane.  Edit and delete only
work after adding a new plug-in to the "items in group" pane.

I have more questions, but this is enough for now.

Charles Lawrence


-----Original Message-----
From: JWLua [mailto:jwlua-bounces at jwmusic.nu] On Behalf Of Jari Williamsson
Sent: Saturday, August 31, 2013 7:03 AM
To: The JW Lua script plug-in.
Subject: [JW Lua] Some Lua power

Hello all!

Virtually everything in the Lua language is based on tables with key/value
pairs. This is one of the reasons why Lua is so easy to transform to
proprietary language dialects. This is also why a R-value for a property
might return nil, if it's undefined/misspelled (it's simply returning "the
element doesn't exist in this table").

The table feature is extremely powerful in its simplicity, and it can
simplify programming tasks quite a lot.

Here's an example on how to use it. The script below counts all the entries
in the selected region, and collects the counters based on the staff the
entry appears on.

If you have used some other computer language, you might notice that there's
no need here to know in advance for how many staves the data should be
collected, and the staff IDs doesn't even need to be in some specific order.

----
-- Collect the number of entries for each staff in
-- the selection in a Lua table
local staffstatistics = {}  -- Create an empty table for e in
eachentry(finenv.Region()) do
     if staffstatistics[e.Staff] == nil then
         staffstatistics[e.Staff] = 1
     else
         staffstatistics[e.Staff] = staffstatistics[e.Staff] + 1
     end
end

-- Print the collected statistics using Lua's pairs() iterator
-- "k" is the key, "v" is the value
for k, v in pairs(staffstatistics) do
     print ("Staff number", k, "has", v, "note entries") end
----

(A key in the table can even be a string, so you could easily use Lua in a
similar way to create a lyrics word counter, to find out how many times a
certain word is used, for example.)

I'll perhaps put this example in the Wiki later on.


Best regards,

Jari Williamsson


_______________________________________________
JWLua mailing list
JWLua at jwmusic.nu
http://jwmusic.nu/mailman/listinfo/jwlua_jwmusic.nu





More information about the JWLua mailing list