webmcp

diff framework/env/param/iterate.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/param/iterate.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,29 @@
     1.4 +--[[--
     1.5 +for
     1.6 +  index,          -- index variable counting up from 1
     1.7 +  prefix          -- prefix string with index in square brackets to be used as a prefix for a key passed to param.get or param.get_list
     1.8 +in
     1.9 +  param.iterate(
    1.10 +    prefix        -- prefix to be followed by an index in square brackets and another key
    1.11 +  )
    1.12 +do
    1.13 +  ...
    1.14 +end
    1.15 +
    1.16 +This function returns an interator function to be used in a for loop. The CGI GET/POST parameter (or internal parameter) with the name "prefix[len]" is read, where 'prefix' is the prefix passed as the argument and 'len' ist just the literal string "len". For each index from 1 to the read length the returned iterator function returns the index and a string consisting of the given prefix followed by the index in square brackets to be used as a prefix for keys passed to param.get(...) or param.get_list(...).
    1.17 +
    1.18 +--]]--
    1.19 +
    1.20 +function param.iterate(prefix)
    1.21 +  local length = param.get(prefix .. "[len]", atom.integer) or 0
    1.22 +  if not atom.is_integer(length) then
    1.23 +    error("List length is not a valid integer or nil.")
    1.24 +  end
    1.25 +  local index = 0
    1.26 +  return function()
    1.27 +    index = index + 1
    1.28 +    if index <= length then
    1.29 +      return index, prefix .. "[" .. index .. "]"
    1.30 +    end
    1.31 +  end
    1.32 +end

Impressum / About Us