How to print elements of array grouped

Hello to all,

Please some help.

I have this array of arrays.

x = [ [“a”,“b”,“c”,“2”], [“d”,“e”,“f”,""], [“g”,“h”,“i”,“j”] ]

And I’m trying to print the Nth element of each array grouped by blocks
like below:

Begin

  • Value a -
  • Value d -
  • Value g -
    End
    Begin
  • Value b -
  • Value e -
  • Value h -
    End
    Begin
  • Value c -
  • Value f -
  • Value i -
    End
    Begin
  • Value 2 -
  • Value -
  • Value j -
    End

I’m trying with this loop but is not working for me.
for j in 0… x.map(&:size).max
p “Begin”
for i in 0…x.size
puts “-” + x[#{i}][#{j}] + “-”
end
p “End”
end

How can this be done?

Thanks in advance

Yes, it is not good expressed.
Meaby you can do this:

x.each do |first_dimension|
first_dimension.each do |item|
puts “Begin”
puts “- #{item} -”
puts “End”
end
end

igorjorobus nearly got it right
i think you are looking for #transpose

x.transpose.each do |first_dimension|
puts “Begin”
first_dimension.each do |item|
puts “- #{item} -”
end
puts “End”
end

Hello Damián and Hans,

Thanks for the help. The code it works, I’ve been trying and I wanted to
know how to print the same when the arrays doesn’t have same size?
For example if X is like below:

x = [ [“a”], [“d”,“e”,“f”], [“g”,“h”] ]

The output for the cases where a value doesn’t exist, would be print
empty like below.

Begin

  • Value a -
  • Value d -
  • Value g -
    End
    Begin
  • Value -
  • Value e -
  • Value h -
    End
    Begin
  • Value -
  • Value f -
  • Value -
    End

Thanks for help so far

Hello Robert,

Thank you. It works just fine. Only how some questions.

May you explain me a little bit how it works “inject” command
to understand better how is getting the max in this line
“x.inject(0){|m,l| [m, l.size].max}.times do |i|”

And…
For the line “x.each {|a| puts “- Value #{a[i]} -”}”
Why is not printing error when prints elements of array “a” that doesn’t
exist?
I mean if I have z=[1,3] and I print z[2], I get “nil”, but in your
code, the nil values are printed as blanck values without any “if
statement”. Is exactly what I want, I only thougth that trying to print
nil values will produce an error.

Thanks again for the help

Von Levix wrote in post #1145295:

Hello Damián and Hans,

Thanks for the help. The code it works, I’ve been trying and I wanted to
know how to print the same when the arrays doesn’t have same size?
For example if X is like below:

x = [ [“a”], [“d”,“e”,“f”], [“g”,“h”] ]

x.inject(0) {|m,l| [m, l.size].max}.times do |i|
puts ‘Begin’
x.each {|a| puts “- Value #{a[i]} -”}
puts ‘End’
end

Thank you Joel.

So “#{…}” converts to string in the same way that x.to_s?

Use IRB to test your theories:

irb(main):001:0> nil.to_s
=> “”
irb(main):002:0> “#{nil}”
=> “”

Von Levix wrote in post #1145485:

May you explain me a little bit how it works “inject” command
to understand better how is getting the max in this line
“x.inject(0){|m,l| [m, l.size].max}.times do |i|”

Enumerable#inject passes on the value obtained as argument to the block
as first argument and then passes the result of the block to the next
iteration etc. finally returning the result of the last block
evaluation.

And…
For the line “x.each {|a| puts “- Value #{a[i]} -”}”
Why is not printing error when prints elements of array “a” that doesn’t
exist?

Joel answered that. You can verify this yourself:

irb(main):004:0> class X; def to_s; $stdout.puts “to_s called”; super;
end; end
=> nil
irb(main):005:0> “before#{X.new}after”
to_s called
=> “before#<X:0x000000008a8478>after”

Cheers

robert

Hello Robert,

Many thanks for your explanation. I’ll experiment folowing the examples
you and Joel shared me.

Best regards