Printing out contents of 2 arrays using a loop

P

Paul Roche

Hi. I'd like to print out the contents of 2 arrays as follows........

print array1[0] array2[0]
print array1[1] array2[1]
etc

array1 holds names
array2 holds occupation

so an example print out will be.......


Joe Bloggs Carpenter
etc


I'm struggling constructing a loop or iteration that prints this out.
The psuedocode is something as follows...

for length of array1
print out contents of array1 and array2

or something similar

Any help is appreciated thanks.
 
J

Jesús Gabriel y Galán

Hi. I'd like to print out the contents of 2 arrays as follows........

print array1[0] array2[0]
print array1[1] array2[1]
etc

array1 holds names
array2 holds occupation

so an example print out will be.......


Joe Bloggs =A0Carpenter
etc


I'm struggling constructing a loop or iteration that prints this out.
The psuedocode is something as follows...

for length of array1
print out contents of array1 and array2

or something similar

Any help is appreciated thanks.

irb(main):001:0> a1 =3D ["joe blogs", "peter smith"]
=3D> ["joe blogs", "peter smith"]
irb(main):002:0> a2 =3D ["carpenter", "policeman"]
=3D> ["carpenter", "policeman"]
irb(main):003:0> a1.zip(a2).each {|name, occupation| puts "#{name} is
a #{occupation}"}
joe blogs is a carpenter
peter smith is a policeman

Another way that avoids creating the intermediate array would be to
iterate using the index:

irb(main):004:0> a1.each_with_index {|name, i| puts "#{name} is a #{a2}"=
}
joe blogs is a carpenter
peter smith is a policeman
=3D> ["joe blogs", "peter smith"]

Jesus.
 
R

Rick DeNatale

Hi. I'd like to print out the contents of 2 arrays as follows........

print array1[0] array2[0]
print array1[1] array2[1]
etc

array1 holds names
array2 holds occupation

so an example print out will be.......


Joe Bloggs =A0Carpenter
etc


I'm struggling constructing a loop or iteration that prints this out.

names =3D %w(joe sam john fred)
occupations =3D %w(tinker tailor soldier spy)

names.zip(occupations).each {|name, occupation| puts "#{name} is a
#{occupation}"}


--=20
Rick DeNatale

Help fund my talk at Ruby Conf 2010:http://pledgie.com/campaigns/13677
Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
R

Robert Klemme

Hi. I'd like to print out the contents of 2 arrays as follows........

print array1[0] array2[0]
print array1[1] array2[1]
etc

array1 holds names
array2 holds occupation

so an example print out will be.......


Joe Bloggs Carpenter
etc


I'm struggling constructing a loop or iteration that prints this out.

names = %w(joe sam john fred)
occupations = %w(tinker tailor soldier spy)

names.zip(occupations).each {|name, occupation| puts "#{name} is a
#{occupation}"}

#zip accepts a block, which avoids creating the merged Array:

irb(main):005:0> names.zip(occupations){|name, occupation| puts "#{name}
is a #{occupation}"}
joe is a tinker
sam is a tailor
john is a soldier
fred is a spy
=> nil

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top