Module:Test: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
-- Main function
-- Main function
local function main()
local function main()
     local filename = "GEPrices/data.json" -- JSON file name
     local filename = ('Module:GEPrices/data.json') -- JSON file name
     local data = readJSON(filename) -- Read data from JSON file
     local data = readJSON(filename) -- Read data from JSON file
     if not data then
     if not data then

Revision as of 22:12, 3 April 2024

Documentation for this module may be created at Module:Test/doc

local json = require("Module:GEPrices/data.json")

-- Function to read data from JSON file
local function readJSON(filename)
    local file = io.open(filename, "r") -- Open file in read mode
    if not file then return nil end -- If file doesn't exist, return nil

    local content = file:read("*a") -- Read the entire content
    file:close() -- Close the file
    return json.decode(content) -- Decode JSON content and return
end

-- Main function
local function main()
    local filename = ('Module:GEPrices/data.json') -- JSON file name
    local data = readJSON(filename) -- Read data from JSON file
    if not data then
        print("Error: Could not read JSON file")
        return
    end

    -- Take input from user
    io.write("Enter the name of the item: ")
    local itemName = io.read()

    -- Search for the item in the data
    local price = data[itemName]

    if price then
        print("Price of", itemName, ":", price)
    else
        print("Item not found")
    end
end

main() -- Run the main function