[PD] manipulation of text files - grep - shell - coll

Frank Barknecht fbar at footils.org
Sat Jan 19 12:19:04 CET 2008


Hallo,
robcanning hat gesagt: // robcanning wrote:

> well my programming skills don't really extend far pass writing the odd 
> bash script but yes, writing an objectclass would be ideal (you mean a 
> pd objectclass?) any pointers on where to start with this using python?

Unless you already have pyext installed, I think installing the Lua
loader is a bit easier. You need to check it out from the subversion
repo at goto10, install the lua5.1-dev packages for your distro, then
on Linux a simple "make" should compile it. Load it with "-lib lua".

Learning Lua or Python is useful in other contexts as well, and
actually it's not that hard to turn that knowledge into a Pd object
class. You may want to start at http://lua-users.org/wiki/LuaTutorial

IMO writing shell scripts is harder than writing Lua/Python scripts.

The general structure for both Python and Lua Pd classes is very
simple. I attached a quickie demonstrating this for Lua: "ltxt" is a
kind of simplified "coll" or "textfile", where you can store data
under float and symbol keys and look it up again. Starting with that
it's easy to extend this with methods to search for keys, sort the
data storage array etc, delete data where keys match a certain pattern
etc. The example is more meant to illustrate the general structure of
a Lua Pd class than as a real object to use.

Ciao
-- 
 Frank Barknecht                                     _ ______footils.org__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ltxt-help.pd
Type: application/puredata
Size: 939 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20080119/ee33c633/attachment.bin>
-------------- next part --------------
-- ltxt: a simple hash type for Pd written in Lua.

local NAME_OF_CLASS = "ltxt" -- should be equal to the name of this file minus suffix

-- register this class with Pd as NAME_OF_CLASS
local M = pd.Class:new():register(NAME_OF_CLASS)

-- M now is used as the name for the class in this file.

function M:initialize(name, args)
    self.outlets = 2
    self.inlets = 1
    self.mydata = {} -- empty table to store whatever
    return true
end

-- lookup key in mydata, bang second outlet if not found:
function M:lookup(x)
    if self.mydata[x] then
        self:outlet(1, "list", self.mydata[x])
    else
        self:outlet(2, "bang", {})
    end
end

-- float and symbol methods will look up key in mydata table:
function M:in_1_float(x)
    self:lookup(x)
end

function M:in_1_symbol(x)
    self:lookup(x)
end

-- what to do on "add [atoms]" into 1st inlet:
function M:in_1_add(atoms)
    local key = table.remove(atoms, 1) -- pop first element of "atoms" into "key"
    self.mydata[key] = atoms
end

-- what to do on "print" into 1st inlet:
function M:in_1_print()
    for k,v in pairs(self.mydata) do
        pd.post(k .. ": " .. table.concat(self.mydata[k], " "))
    end
end

-- what to do on "reset" into 1st inlet:
function M:in_1_reset()
    self.mydata = {}
end


More information about the Pd-list mailing list