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?

···

--
Posted via http://www.ruby-forum.com/.

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

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

-----Messaggio originale-----

···

Da: list-bounce@example.com [mailto:list-bounce@example.com] Per conto di
Bob Sanders
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?
--
Posted via http://www.ruby-forum.com/.

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

···

On Aug 18, 2007, at 11:55 AM, Bob Sanders 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)

Bob Sanders 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 :slight_smile:

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

Actually:

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

···

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

On Saturday 18 August 2007 02:55:17 am Bob Sanders 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}

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

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

···

On Aug 18, 2007, at 4:55 AM, Bob Sanders wrote:

Let's say I have an array defined as:

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

You could use Rio for this problem:

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

:wink:

···

On Aug 18, 4:09 am, Konrad Meyer <kon...@tylerc.org> 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?

Hi,

···

Am Samstag, 18. Aug 2007, 18:55:17 +0900 schrieb Bob Sanders:

How would I then print out the first 5 records of that array?

Depending on what you want to do further maybe this is worth
a consideration:

  5.times { puts @people.shift }

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Bob Sanders 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?

5.times{|t| puts @people[t]}

···

--
Posted via http://www.ruby-forum.com/\.

Stefano Crocco wrote:

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!

···

--
Posted via http://www.ruby-forum.com/\.

Stefano Crocco wrote:

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}

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

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Sat, 18 Aug 2007, Stefan Rusterholz wrote:

Bob Sanders 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 :slight_smile:

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)

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 <eric-news@xs4all.nl>:

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

>
> @people[0..4].each { |item| puts item }
>
> -----Messaggio originale-----
> Da: list-bounce@example.com [mailto:list-bounce@example.com] Per conto
di
> Bob Sanders
> 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?

I'd do it like this:
> puts @people[0..4]
Jim
Jen
Jess
Jay
Jack
=> nil

Is the .each construct better?
--
Groeten,
Eric

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[i]}
0.upto(4) {|i| puts @people[i]}

# 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

···

On 18.08.2007 13:14, Konrad Meyer wrote:

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

On Saturday 18 August 2007 02:55:17 am Bob Sanders 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}

Actually:

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

Bertram Scharpf schrieb:

Hi,

How would I then print out the first 5 records of that array?

Depending on what you want to do further maybe this is worth
a consideration:

  5.times { puts @people.shift }

Bertram

No need to iterate here, just
  puts @people.slice!(0,5)

but personally I like
  puts @people.first(5)
the most...

Regards
Florian

···

Am Samstag, 18. Aug 2007, 18:55:17 +0900 schrieb Bob Sanders:

Dirk Bruijn wrote:

Stefano Crocco wrote:

> puts @people [0..4]

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

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

···

--
NP: Nevermore - Tomorrow Turned Into Yesterday
Jabber: sepp2k@jabber.org
ICQ: 205544826

David A. Black wrote:

Hi --

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

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 :slight_smile:

Regards
Stefan

···

On Sat, 18 Aug 2007, Stefan Rusterholz wrote:

--
Posted via http://www.ruby-forum.com/\.

Dirk Bruijn wrote:

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

    5.times{ puts @people.shift }

Hey - they didn't say non-destructively!

···

--
  Phlip
  Test Driven Ajax (on Rails) [Book]
  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax