IO.puts tuple item with string

How do I print a string and a tuple item together using IO.puts? For example {aaa, b, c} = {‘d’, 2, 3}

IO.puts aaa with the string ‘ggg’

I dont quite understand what you intend to do. Can you please give some code lines what you expect to get?

If I understand you correctly, you want to do this:

{aaa, b, c} = {"d", 2, 3}
IO.puts "#{aaa}ggg"

This is called String interpolation.

2 Likes

{aaa, b, c} = {‘d’, 2, 3}

Then I want to output,

aaa equals ‘d’

on the screen

here are 3 ways:

  def tuple_sample do
    {aaa, _, _} = {'d', 2, 3}
    "aaa equals '#{aaa}'"
  end

  def tuple_sample2 do
    tuple = {'d', 2, 3}
    "aaa equals '#{elem(tuple, 0)}'"
  end

  def tuple_sample3 do
    aaa = {'d', 2, 3} |> elem(0)
    "aaa equals '#{aaa}'"
  end