How do you print the first 5 records in an array?

B

Bob Sanders

Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?
 
S

Stefano Crocco

Alle sabato 18 agosto 2007, Bob Sanders ha scritto:
Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

puts @people [0..4]

Stefano
 
B

Bob Sanders

Stefano said:
Alle sabato 18 agosto 2007, Bob Sanders ha scritto:
Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

puts @people [0..4]

Stefano

Stefano, you're the best. Thank you!
 
D

Dirk Bruijn

Stefano said:
Alle sabato 18 agosto 2007, Bob Sanders ha scritto:
Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

puts @people [0..4]

Stefano
0.upto(4) { |x| puts @people[x]}
 
X

Xavier Noria

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e.
from
Jim to Jack)

You can explicitly say "Take n items starting from index i" with the
syntax

array[i, n]

In your example that'd give

puts @people[0, 5]

-- fxn
 
B

Bob Sanders

You all are so helpful. This is why the ruby community rocks. Thank you
so much :)
 
S

Stefan Rusterholz

Bob said:
Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

puts(*@people.first(5))
Ruby, so surprisingly unsurprising :)

Regards
Stefan
 
K

Konrad Meyer

--DSPAM_MULTIPART_EX-13582
Content-Type: multipart/signed;
boundary="nextPart1398676.4MVO2BJ6Cx";
protocol="application/pgp-signature";
micalg=pgp-sha1

--nextPart1398676.4MVO2BJ6Cx
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Let's say I have an array defined as:
=20
@people =3D ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]
=20
How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)
=20
Any ideas?

One idea comes to mind:

@people.inject([]){|a,p|a<<p if a.length<5;a}.each{|p|puts p}

Anyone wanna throw in callcc too?
Cheers,
=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart1398676.4MVO2BJ6Cx
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGxtNhCHB0oCiR2cwRApkrAJ0Vs5pCf5DM9Oos/d/rdH6eqt4YpACgw/dZ
gHaqlc4CwQSj3JU+M1JWhAc=
=FbuK
-----END PGP SIGNATURE-----

--nextPart1398676.4MVO2BJ6Cx--

--DSPAM_MULTIPART_EX-13582
Content-Type: text/plain
X-DSPAM-Signature: 46c6d363135821228095555

!DSPAM:46c6d363135821228095555!
--DSPAM_MULTIPART_EX-13582--
 
K

Konrad Meyer

--DSPAM_MULTIPART_EX-22425
Content-Type: multipart/signed;
boundary="nextPart5574683.0arGV5aQXf";
protocol="application/pgp-signature";
micalg=pgp-sha1

--nextPart5574683.0arGV5aQXf
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Let's say I have an array defined as:
=20
@people =3D ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]
=20
How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)
=20
Any ideas?
=20
One idea comes to mind:
=20
@people.inject([]){|a,p|a<<p if a.length<5;a}.each{|p|puts p}

Actually:

$><<@people.inject([]){|a,p|a<<p if a.length<5;a}*"\n"

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart5574683.0arGV5aQXf
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGxtSxCHB0oCiR2cwRAmxgAJ47CW7vRkxMhHW1JFxeP1JDEktZXwCg163Y
tkKEJlwVlgO78shz1t7TEHQ=
=zA7l
-----END PGP SIGNATURE-----

--nextPart5574683.0arGV5aQXf--

--DSPAM_MULTIPART_EX-22425
Content-Type: text/plain
X-DSPAM-Signature: 46c6d4b4224251969219952

!DSPAM:46c6d4b4224251969219952!
--DSPAM_MULTIPART_EX-22425--
 
D

David A. Black

Hi --

Bob said:
Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

puts(*@people.first(5))
Ruby, so surprisingly unsurprising :)

I don't think you need the * there.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

David A. Black

Hi --

Sebastian said:
Dirk said:
Stefano Crocco wrote:


puts @people [0..4]

0.upto(4) { |x| puts @people[x]}

Why do you feel that that's preferable to Stefano's solution?
It's not. I mistakenly placed my reaction on the wrong leaf.

I'd avoid iterating through a whole array just to puts its elements,
but in this case your code avoids creating a temporary sub-array. So
there might be some reason to use that technique.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
S

Stefan Rusterholz

David said:
Hi --



I don't think you need the * there.


David

Darn, and just when you say it is unsurprising you hit a surprising
thing.
Right, I keep forgetting that puts treats Arrays differently than every
other object.
Thanks for pointing out :)

Regards
Stefan
 
R

Robert Klemme

Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?
One idea comes to mind:

@people.inject([]){|a,p|a<<p if a.length<5;a}.each{|p|puts p}

Actually:

$><<@people.inject([]){|a,p|a<<p if a.length<5;a}*"\n"

Although I'm always fond of solutions using #inject in this case I'm
sorry to say that but this is not only inefficient but totally
obfuscated. If you feel you have to iterate then you can immediately
print members. These are my preferred solutions

# fast and readable
5.times {|i| puts @people}
0.upto(4) {|i| puts @people}

# might be less efficient but
# very readable
puts @people[0,5]
puts @people[0...5]
puts @people[0..4]

Note: it's not too unlikely that those variants with the sub arrays are
actually faster, because that is all done in C code.

Cheers

robert
 
J

James Edward Gray II

Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

I see you got your answer, but I wanted to add that you can do the
above with a lot less noise:

@people = %w[Jim Jen Jess Jay Jack John Jeff Jed Jill]

James Edward Gray II
 
R

rio4ruby

One idea comes to mind:

@people.inject([]){|a,p|a<<p if a.length<5;a}.each{|p|puts p}

Anyone wanna throw in callcc too?

You could use Rio for this problem:

require 'rio'
rio(?-) < @people[0...5].join($/) + $/

;)
 
D

David A. Black

Hi --

puts @people[0..4].join("\n")

Assuming people don't end with "\n", that will have the same effect as
doing it without the join:

irb(main):007:0> people = %w{ me you him her them }
=> ["me", "you", "him", "her", "them"]
irb(main):008:0> puts people[0..4].join("\n")
me
you
him
her
them
=> nil
irb(main):009:0> puts people[0..4]
me
you
him
her
them
=> nil

If they do end with "\n", then they'll be separated by blank lines if
you do the join.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top