如何在Elixir中打印出地图的数组值? [英] How to print out a map's array values in Elixir?

查看:89
本文介绍了如何在Elixir中打印出地图的数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Ruby到Elixir的新手,我正在尝试使用以下代码打印出地图的数组值:

New to Elixir from Ruby and I'm trying to print out a map's array values using the following code:

map-script.ex

list = [0, 1]
map = %{0 => [1, 2], 1 => [2, 3]}

Enum.each list, fn n ->
  IO.puts map[n]
end

输出:

^A^B
^B^C

我在做什么错? Elixir看起来与Ruby类似,但是行为却有所不同。...

What am I doing wrong? Elixir looks similar to Ruby but it behaves differently....

推荐答案

您应该使用 IO.inspect 而不是 IO.puts 来打印值的内部表示形式:

You should use IO.inspect instead of IO.puts to print the internal representation of a value:

iex> IO.puts [1, 2]
^A^B                          # prints "^A^B"
:ok                           # return value of IO.puts

iex> IO.inspect [1, 2]
[1, 2]                        # prints "[1, 2]"
[1, 2]                        # return value of IO.inspect

但是,您仍然可能会遇到一些特殊值的问题:

However you might still get problems with some special values:

iex> IO.inspect [97, 98]
'ab'                          # prints "'ab'"
'ab'                          # return value of IO.inspect

要解释此行为,我们需要了解字符串在Elixir中的工作方式。有两种字符串。二进制字符串(双引号)和字符列表(单引号)。在内部,这些是从更简单的基元构建的。二进制字符串是从二进制文件构建的,而字符列表是从列表构建的。

To explain this behavior we need to understand how strings work in Elixir. There are two kinds of strings. Binary strings (double quoted) and character lists (single quoted). Internally, these are built from simpler primitives. Binary strings are built from binaries, and character lists are built from lists:

iex> "ab"                    # a binary string
"ab"

iex> <<97, 98>>              # the same string in binary syntax
"ab"

iex> 'ab'                    # a character list
'ab'

iex> [97, 98]                # the same character list in list syntax
'ab'

它乍一看可能令人困惑,但这是由于两件事。首先,没有内置的字符串类型,因为我们已经看到字符串是从其他原始类型构建的。其次,Elixir中没有用户定义的类型。因此,当Elixir看到仅包含整数的列表时,为了方便起见,它将尝试将其打印为字符串。但是,实际上,它仍然只是整数列表。

It might seem confusing at first, but this happens due to two things. First, there is no built in string type, as we have seen strings are built from other primitive types. Second, there are no user defined types in Elixir. So when Elixir sees a list with only integers, it tries to print it as a string for convenience. However, under the hood it is still just a list of integers.

在上面的示例中, 97 98 表示字符 a b 的Unicode代码点因此即使使用 IO.inspect ,它们也将显示为字符串。

In the examples above, 97 and 98 represent the unicode code points for the characters a and b so these will be shown as a string, even if using IO.inspect.

现在,您可以了解为什么在您的示例中打印 ^ A ^ B –这些仅仅是 ASCII编码,恰好由代码点 1 2 表示。

Now you can see why it printed ^A^B in your examples – these are just control characters from the ASCII encoding, which happen to be represented by the codepoints 1 and 2.

但是,您可以通过选项 char_lists::as_lists 来打印原始列表,而无需进行这种转换:

However, you can pass the option char_lists: :as_lists in order to print raw lists without an attempt to do such a conversion:

iex> IO.inspect [97, 98], char_lists: :as_lists
[97, 98]                                         # prints '[97, 98]'
'ab'                                             # Iex still shows the return value as 'ab'

如果打开 iex 并键入 h Inspect.Opts ,您将看到Elixir也使用其他值(特别是结构和二进制文件)来进行此类操作。

If you open up iex and type h Inspect.Opts, you will see that Elixir does this kind of thing with other values as well, specifically structs and binaries.

这篇关于如何在Elixir中打印出地图的数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆