Forum: Ruby How to print elements of array grouped

F1fab6b010c012052f207c17b0385ba4?d=identicon&s=25 Von Levix (vostlex)
on 2014-05-06 22:20
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
5b972395a92333843018b4add8af0437?d=identicon&s=25 Damián M. González (igorjorobus)
on 2014-05-07 04:38
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
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2014-05-07 06:43
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
F1fab6b010c012052f207c17b0385ba4?d=identicon&s=25 Von Levix (vostlex)
on 2014-05-07 18:23
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-05-08 09:34
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
F1fab6b010c012052f207c17b0385ba4?d=identicon&s=25 Von Levix (vostlex)
on 2014-05-09 01:58
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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2014-05-09 22:05
Use IRB to test your theories:

irb(main):001:0> nil.to_s
=> ""
irb(main):002:0> "#{nil}"
=> ""
F1fab6b010c012052f207c17b0385ba4?d=identicon&s=25 Von Levix (vostlex)
on 2014-05-09 22:14
Thank you Joel.

So "#{..}" converts to string in the same way that x.to_s?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-05-10 17:39
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
F1fab6b010c012052f207c17b0385ba4?d=identicon&s=25 Von Levix (vostlex)
on 2014-05-12 07:36
Hello Robert,

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

Best regards
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.