Module:Test: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local json = require("Module:GEPrices/data.json")
local json = require("GEPrices/data.json")


-- Function to read data from JSON file
-- Function to read data from JSON file

Revision as of 22:08, 3 April 2024

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

local json = require("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 = "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