从 Elixir 脚本的 Map 中检索值
Retrieving values from a Map from an Elixir script
如果我在 Elixir 中写一个简单的 Map,例如:
person = %{ :name => "Bob", :age => 45}
我将它保存为脚本,例如
script.exs
使用
编译脚本后如何检索 Bob 的年龄
elixir script.exs
?
或者,甚至更好:
iex script.exs
如果我再写person[:age]
它给我一个错误:
** (CompileError) iex:1: undefined function person/0
在 Elixir 中不能使用这样的地图吗?
有点老套,但您可以使用 iex --dot-iex script.exs
传递脚本。参见 The .iex.exs file。
$ iex --dot-iex script.exs
Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> person
%{age: 45, name: "Bob"}
How do I retrieve Bob's age after I compile the script with elixir script.exs
?
不确定你的意思。在您 运行 脚本之后,脚本已经完成,因此无法检索任何值(除非脚本 returns 或设置环境)。
如果我在 Elixir 中写一个简单的 Map,例如:
person = %{ :name => "Bob", :age => 45}
我将它保存为脚本,例如
script.exs
使用
编译脚本后如何检索 Bob 的年龄elixir script.exs
?
或者,甚至更好:
iex script.exs
如果我再写person[:age]
它给我一个错误:
** (CompileError) iex:1: undefined function person/0
在 Elixir 中不能使用这样的地图吗?
有点老套,但您可以使用 iex --dot-iex script.exs
传递脚本。参见 The .iex.exs file。
$ iex --dot-iex script.exs
Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> person
%{age: 45, name: "Bob"}
How do I retrieve Bob's age after I compile the script with
elixir script.exs
?
不确定你的意思。在您 运行 脚本之后,脚本已经完成,因此无法检索任何值(除非脚本 returns 或设置环境)。