How to capture and store data (and view it via an alias)?

Vooku
Posts: 72
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku »

I think if I could type @aucdata XXXX (date, item, buyer, price) that would be good enough. I think I could 'mess' with the code to maybe add more columns later if needed. I'm thinking that information would be plenty tho.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox »

Right, so see the post immediately before this one, and you will see what should let you add each of those as options.

Vooku
Posts: 72
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku »

Thanks much.

I'm getting an error on the "if a.[order[1]] ~= b.[order[1]] then return a.[order[1]] < b.[order[1]] end" line.

Says expected a ')' near '['

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox »

Oh. I’m an idiot. I forgot to remove the periods after the a and b when I put in the square brackets. Take out all those periods in each of the three lines, and it should work. Also, just to be safe, you might want to put some spaces between the double closing square brackets, so it can’t misinterpret them.

Vooku
Posts: 72
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku »

Errors all gone, but when I '@aucdata price' it still lists everything by item.

I'm guessing this line isn't correct...

function show_auction_data()

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox »

Yeah, you will notice in my post that it has function show_auction_data(sort_by)

Vooku
Posts: 72
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku »

Hmmm. Still always prints the data the same way no matter if I use:

@aucdata date
@aucdata price
@aucdata item
@aucdata buyer

Sorry for being such a nub:P

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox »

Okay, show me what you have for both the alias and the script. Hopefully I'll be able to spot what is going wrong.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: How to capture and store data (and view it via an alias)?

Post by Jor'Mox »

Also, after testing out the script with the bit of data you provided earlier (and it does seem to work for me), I noticed that the prices were being sorted as strings rather than numbers (because they are stored as strings, not numbers). So, I put in something to convert prices to numbers when they are collected, as you can see here (also notice the assorted sorting options I added in just for testing purposes):
Code: [show] | [select all] lua
auction_data = auction_data or {}

function add_auction_data(item, price, buyer)
    local date = os.date("%b %d, %Y")
    price = tonumber(price)
    table.insert(auction_data,{item = item, price = price, buyer = buyer, date = date})
    table.save(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end

function show_auction_data(sort_by)
    local sort_func
    sort_by = sort_by or "default"
    local sort_orders = {
        default = {'item','price','date'},
        date = {'date','item','price'},
        price = {'price','item','date'},
        buyer = {'buyer','item','date'},
    }
    local order = sort_orders[sort_by] or sort_orders.default
    
    sort_func = function (a,b)
            if a[ order[1] ] ~= b[ order[1] ] then return a[ order[1] ] < b[ order[1] ] end
            if a[ order[2] ] ~= b[ order[2] ] then return a[ order[2] ] < b[ order[2] ] end
            if a[ order[3] ] ~= b[ order[3] ] then return a[ order[3] ] < b[ order[3] ] end
            return false
        end
    table.sort(auction_data,sort_func)
    print(string.format("%-50s: %5s, %12s, %s", "ITEM", "PRICE", "DATE", "BUYER"))
    for k,v in ipairs(auction_data) do
        print(string.format("%-50s: %5s, %12s, %s",v.item,v.price,v.date,v.buyer))
    end
end

if io.exists(getMudletHomeDir() .. "/auction_data.lua") and table.is_empty(auction_data) then
    table.load(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end
But, you would also need to run through the existing auction data and convert existing prices from strings into numbers. To do that, I used the following command: lua for k,v in ipairs(auction_data) do auction_data[k].price = tonumber(v.price) end

Vooku
Posts: 72
Joined: Sun Aug 21, 2016 2:42 pm

Re: How to capture and store data (and view it via an alias)?

Post by Vooku »

Here is what I have...
Code: [show] | [select all] lua
auction_data = auction_data or {}

function add_auction_data(item, price, buyer)
    local date = os.date("%b %d, %Y")
    table.insert(auction_data,{item = item, price = price, buyer = buyer, date = date})
    local sort_func = function (a,b)
            if a.item ~= b.item then return a.item < b.item end
            if a.price ~= b.price then return a.price < b.price end
            if a.date ~= b.date then return a.date < b.date end
            return false
        end
    table.sort(auction_data,sort_func)
    table.save(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end

if io.exists(getMudletHomeDir() .. "/auction_data.lua") and table.is_empty(auction_data) then
   table.load(getMudletHomeDir() .. "/auction_data.lua", auction_data)
end

function show_auction_data(sort_by)
    local sort_func
    sort_by = sort_by or "default"
    local sort_orders = {
        default = {'item','price','date'},
        date = {'date','item','price'},
    }
    local order = sort_orders[sort_by] or sort_orders.default
    
    sort_func = function (a,b)
            if a[order[1]] ~= b[order[1]] then return a[order[1]] < b[order[1] ] end
            if a[order[2]] ~= b[order[2]] then return a[order[2]] < b[order[2] ] end
            if a[order[3]] ~= b[order[3]] then return a[order[3]] < b[order[3] ] end
            return false
        end
    table.sort(auction_data,sort_func)
    print(string.format("%-60s: %8s, %8s, %s", "ITEM", "PRICE", "DATE", "    BUYER"))
    for k,v in ipairs(auction_data) do
        print(string.format("%-60s: %8s, %8s, %s",v.item,v.price,v.date,v.buyer))
    end
end

Post Reply