zoukankan      html  css  js  c++  java
  • 面试lua笔试题各种坑

    替换字符串"abcdefgh" 中的"abc"为"ddc" 

    local str ="abcdefgh";

    b = string.gsub(str, "%abc", "ddc");

    str = b;

    print(str);

    https://my.oschina.net/workhelu/blog/363949

    pairs

    1.pairs遍历table中的所有的key-vale 而ipairs会根据key的数值从1开始加1递增遍历对应的table[i]值

    a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",}
    for key, value in ipairs(a) do
      print(key, value)
    end
    
    
    结果:
    1	a1
    2	a2
    3	a3
    
    a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",}
    for key, value in pairs(a) do
      print(key, value)
    end
    结果:
    6	a5
    2	a2
    3	a3
    1	a1
    5	a4


    function clone( object )
    local lookup_table = {}
    local function copyObj( object )
    if type( object ) ~= "table" then
    return object
    elseif lookup_table[object] then
    return lookup_table[object]
    end

    local new_table = {}
    lookup_table[object] = new_table
    for key, value in pairs( object ) do
    new_table[copyObj( key )] = copyObj( value )
    end
    return setmetatable( new_table, getmetatable( object ) )
    end
    return copyObj( object )
    end

    local t1 = { "a","b","c"}
    -- local t2 = clone(t1);
    --t1[1] = "abc";

    local t2 = t1;
    t1[1] = "abc";
    for i=1,#t2 do
    print(t2[i]);
    end

  • 相关阅读:
    《软件需求十步走》读书笔记二
    《软件需求十步走》读书笔记一
    FJUTOJ-周赛2016-12-16
    FJUTOJ-周赛2016-11-25
    网络爬虫
    树链剖分讲解
    HDU 5266 pog loves szh III
    HDU 3518 Boring counting
    HDU 5929 Basic Data Structure
    HDU 1055 Color a Tree
  • 原文地址:https://www.cnblogs.com/pipicfan/p/9885479.html
Copyright © 2011-2022 走看看