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

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?

Alle sabato 18 agosto 2007, Bob S. 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 C. wrote:

Alle sabato 18 agosto 2007, Bob S. 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!

Stefano C. wrote:

Any ideas?

puts @people [0…4]

Stefano

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

@people[0…4].each { |item| puts item }

-----Messaggio originale-----
Da: [email protected] [mailto:[email protected]] Per conto
di
Bob S.
Inviato: sabato 18 agosto 2007 11.55
A: ruby-talk ML
Oggetto: How do you print the first 5 records in an array?

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?

On Aug 18, 2007, at 11:55 AM, Bob S. wrote:

@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]

puts @people[0, 5]

– fxn

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

Dirk Bruijn wrote:

Stefano C. wrote:

puts @people [0…4]

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

Why do you feel that that’s preferable to Stefano’s solution?

Bob S. wrote:

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

On Saturday 18 August 2007 04:09:16 am Konrad M. wrote:

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"

On Saturday 18 August 2007 02:55:17 am Bob S. wrote:

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}

Anyone wanna throw in callcc too?
Cheers,

Sebastian H. wrote:

Why do you feel that that’s preferable to Stefano’s solution?

It’s not. I mistakenly placed my reaction on the wrong leaf.

Op za 18 aug 12:03:56 2007 CEST schreef Andrea Forlin in
de nieuwsgroep ‘comp.lang.ruby’:

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?

I’d do it like this:

puts @people[0…4]
Jim
Jen
Jess
Jay
Jack
=> nil

Is the .each construct better?

Hi –

On Sat, 18 Aug 2007, Stefan R. wrote:

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

I don’t think you need the * there.

David

Hi –

On Sat, 18 Aug 2007, Dirk Bruijn wrote:

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

David A. Black wrote:

Hi –

On Sat, 18 Aug 2007, Stefan R. wrote:

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

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

On 18.08.2007 13:14, Konrad M. wrote:

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

5.times {|i| puts @people[i]}
0.upto(4) {|i| puts @people[i]}

# might be less efficient but

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

Dirk Bruijn wrote:

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

``````5.times{ puts @people.shift }
``````

Hey - they didn’t say non-destructively!

if you need only to puts on screen you can use the solution of stefano,
but
it’s usefull??

Maybe you need to search, operate on the range item, i think.

Bye

2007/8/18, Eric [email protected]:

On Aug 18, 4:09 am, Konrad M. [email protected] wrote:

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(\$/) + \$/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.