10minutemail
10minutemail

Reputation: 23

How can I get last title from RSS?

I'm using lua-feeds ( http://code.matthewwild.co.uk/lua-feeds/ )

require "lua-feeds/feeds"

local feed = feeds.open("http://php.net/feed.atom");
for _, entry in ipairs(feed) do
bot.rooms["[email protected]"]:send_message(entry:get_child("title"):get_text().."\n"..entry:get_child("link").attr.href);
end

This is my code, that is getting full RSS. I only want to get the last title and link, how can I do that?

Upvotes: 2

Views: 516

Answers (2)

Puppy
Puppy

Reputation: 146940

You can just call the result of ipairs(feed) once.

Upvotes: 0

catwell
catwell

Reputation: 7020

I don't understand how @DeadMG's answer was accepted. It looks terribly wrong to me.

ipairs takes a sequence and returns an iterator over it, so ipairs(feed) is an iterator over the feed. ipairs is called once in the example provided by the OP. The resulting iterator is called several times.

@DeadMG's idea was probably to call the iterator only once. This is how it would look:

local f, v, i = ipairs(feed)
local _, entry = f(v, i)

of, if you like tricks:

local _, _, entry = pcall(ipairs(feed))

But... Why would you do that? You have the feed as a table. You want the first entry of that table:

local entry = feed[1]

Done.

Upvotes: 5

Related Questions