lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Well, it depends on the kind of data you're storing.  But let's say
you're storing a file in a table, where each line is its own entry:

local lines = {}
for line in f:lines() do
  table.insert(lines, line)
end

Now let's say your program wants to know what line(s) a word is on.  You
can create an index that allows you to quickly lookup a word and find
that out:

local index = {}
for line_no, line in ipairs(lines) do
  for word in string.gmatch(line, '[^%s]+') do
    local index_lines = index[word]
    if not index_lines then
      index_lines = {}
      index[word] = index_lines
    end
    table.insert(index_lines, line_no)
  end
end

(By the way, I haven't tested this code, so forgive me if it doesn't
work perfectly)

Now instead of crawling over the table, you lookup the word:

index['foo'] = { ... }

-Rob

On Fri, 08 Apr 2011 23:56:52 +0200
Henning Diedrich <hd2010@eonblast.com> wrote:

> On 4/8/11 11:12 PM, Rob Hoelz wrote:
> > you could probably get away with maintaining an external index.
> 
> Thanks Rob -- not sure how you mean that. How would that look?
> 
> Henning

Attachment: signature.asc
Description: PGP signature