设为首页 加入收藏

TOP

《Terraform 101 从入门到实践》 Functions函数(四)
2023-07-26 08:16:29 】 浏览:120
Tags:Terraform 101 从入门 Functions 函数

length获取字符串、列表、Map等的长度:

> length([])
0
> length(["pkslow"])
1
> length(["pkslow", "com"])
2
> length({pkslow = "com"})
1
> length("pkslow")
6

lookup(map, key, default)根据key值在map中找到对应的value值,如果没有则返回默认值:

> lookup({name = "Larry", age = 18}, "age", 1)
18
> lookup({name = "Larry", age = 18}, "myAge", 1)
1

matchkeys(valueslist, keyslist, searchset)对key值进行匹配。匹配到key值后,返回对应的Value值。

> matchkeys(["a", "b", "c", "d"], [1, 2, 3, 4], [2, 4])
tolist([
  "b",
  "d",
])

merge合并Map,key相同的会被最后的覆盖:

> merge({name = "Larry", webSite = "pkslow.com"}, {age = 18})
{
  "age" = 18
  "name" = "Larry"
  "webSite" = "pkslow.com"
}
> merge({name = "Larry", webSite = "pkslow.com"}, {age = 18}, {age = 13})
{
  "age" = 13
  "name" = "Larry"
  "webSite" = "pkslow.com"
}

one取集合的一个元素,如果为空则返回null;如果只有一个元素,则返回该元素;如果多个元素,则报错:

> one([])
null
> one(["pkslow"])
"pkslow"
> one(["pkslow", "com"])
?
│ Error: Invalid function argument
│ 
│   on <console-input> line 1:
│   (source code not available)
│ 
│ Invalid value for "list" parameter: must be a list, set, or tuple value with either zero or one elements.
?

range生成顺序列表:

range(max)
range(start, limit)
range(start, limit, step)

> range(3)
tolist([
  0,
  1,
  2,
])
> range(1, 6)
tolist([
  1,
  2,
  3,
  4,
  5,
])
> range(1, 6, 2)
tolist([
  1,
  3,
  5,
])

reverse反转列表:

> reverse([1, 2, 3, 4])
[
  4,
  3,
  2,
  1,
]

setintersection对set求交集:

> setintersection([1, 2, 3], [2, 3, 4], [2, 3, 6])
toset([
  2,
  3,
])

setproduct列出所有组合可能:

> setproduct(["Larry", "Harry"], ["Deng", "Potter"])
tolist([
  [
    "Larry",
    "Deng",
  ],
  [
    "Larry",
    "Potter",
  ],
  [
    "Harry",
    "Deng",
  ],
  [
    "Harry",
    "Potter",
  ],
])

setsubtract:set的减法

> setsubtract([1, 2, 3], [3, 4])
toset([
  1,
  2,
])

# 求不同
> setunion(setsubtract(["a", "b", "c"], ["a", "c", "d"]), setsubtract(["a", "c", "d"], ["a", "b", "c"]))
[
  "b",
  "d",
]

setunion:set的加法

> setunion([1, 2, 3], [3, 4])
toset([
  1,
  2,
  3,
  4,
])

slice(list, startindex, endindex)截取列表部分,包括startindex,但不包括endindex:

> slice(["a", "b", "c", "d", "e"], 1, 4)
[
  "b",
  "c",
  "d",
]

sort对列表中的字符串进行排序,要注意如果输入的是数字,会先转化为字符串再排序:

> sort(["larry", "pkslow", "com", "deng"])
tolist([
  "com",
  "deng",
  "larry",
  "pkslow",
])
> sort([3, 6, 1, 9, 12, 79, 22])
tolist([
  "1",
  "12",
  "22",
  "3",
  "6",
  "79",
  "9",
])

sum求和:

> sum([3, 1.2, 9, 17.3, 2.2])
32.7

transpose对Map的key和value进行换位:

> transpos
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【8086汇编入门】《零基础入门学.. 下一篇Java线程中断

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目