[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Cleaner way to do this?

Harry Ohlsen

11/5/2004 1:24:00 AM

> -----Original Message-----
> From: Wayne Vucenic [mailto:nightphotos@gmail.com]
> Sent: Friday, 5 November 2004 12:12
> To: ruby-talk ML
> Subject: Re: Cleaner way to do this?
>
> How about
>
> [1, 2, 3, 4, 5].each_with_index do |x, i|
> if i == 0
> print x
> else
> print ", ",x
> end
> end

That's what I initially suggested :-).

However, he felt it was a bit crazy to do the test every time, given the
large size of the array.

H.


************************************************************************

If you have received this e-mail in error, please delete it and notify the sender as soon as possible. The contents of this e-mail may be confidential and the unauthorized use, copying, or dissemination of it and any attachments to it, is prohibited.

Internet communications are not secure and Hyperion does not, therefore, accept legal responsibility for the contents of this message nor for any damage caused by viruses. The views expressed here do not necessarily represent those of Hyperion.

For more information about Hyperion, please visit our Web site at www.hyperion.com





6 Answers

Zach Dennis

11/5/2004 3:01:00 AM

0

Harry Ohlsen wrote:

>> -----Original Message-----
>> From: Wayne Vucenic [mailto:nightphotos@gmail.com]
>> Sent: Friday, 5 November 2004 12:12
>> To: ruby-talk ML
>> Subject: Re: Cleaner way to do this?
>>
>> How about
>>
>> [1, 2, 3, 4, 5].each_with_index do |x, i|
>> if i == 0
>> print x
>> else
>> print ", ",x
>> end
>> end
>>
>>
>
>That's what I initially suggested :-).
>
>However, he felt it was a bit crazy to do the test every time, given the
>large size of the array.
>
>H.
>

If your friend knows the processing he wants to do on the first element,
why not just keep it simple...


arr = [1,2,3,4,5]
process = proc{ |e| print e }.call( arr[0] )
arr[1...arr.length].each do |e|
print ", #{e}"
end

Zach









Zach Dennis

11/5/2004 4:22:00 AM

0

Zach Dennis wrote:

>
> If your friend knows the processing he wants to do on the first
> element, why not just keep it simple...
>
>
> arr = [1,2,3,4,5]
> process = proc{ |e| print e }.call( arr[0] )
> arr[1...arr.length].each do |e|
> print ", #{e}"
> end
>
This is also faster, then doing a comparison in each iteration :

RESULTS:

---------- Capture Output ----------
"C:\ruby\bin\ruby.exe" -d harry_ohlsen.rb
WRITING 1
Took:
19.297000169754
seconds
WRITING 2
Took: 11.7650001049042 seconds
Terminated with exit code 0

--------CODE-----------
require 'time'

arr = []
2000000.times do |i|
arr << "abcdefghijklmnopqrstuvwxyz"
end

f = File.new( "temp.txt" , "w+" )
t = Time.new
arr.each_with_index do |x,i|
if i==0
f.print x
else
f.print ', ',x
end
end
f.close
puts "WRITING 1"
puts "Took: ", (Time.new.to_f - t.to_f).to_s, " seconds"

f = File.new( "temp.txt2" , "w+" )
t = Time.new
process = proc { |e| f.print e }.call( arr[0] )
arr[1...arr.length].each do |e|
f.print ', ',e
end
f.close
puts "WRITING 2"
print "Took: ", (Time.new.to_f- t.to_f).to_s, " seconds"



benny

11/5/2004 1:33:00 PM

0

Harry Ohlsen wrote:

>> -----Original Message-----
>> From: Wayne Vucenic [mailto:nightphotos@gmail.com]
>> Sent: Friday, 5 November 2004 12:12
>> To: ruby-talk ML
>> Subject: Re: Cleaner way to do this?
>>
>> How about
>>
>> [1, 2, 3, 4, 5].each_with_index do |x, i|
>> if i == 0
>> print x
>> else
>> print ", ",x
>> end
>> end
>
> That's what I initially suggested :-).
>
> However, he felt it was a bit crazy to do the test every time, given the
> large size of the array.
>

how about this?

arr = [1, 2, 3, 4, 5]
first = arr.shift
print arr.inject(first) { | c, i | "#{c.to_s}, #{i.to_s}" }

benny

benny

11/5/2004 1:57:00 PM

0

benny wrote:

> Harry Ohlsen wrote:
>
>>> -----Original Message-----
>>> From: Wayne Vucenic [mailto:nightphotos@gmail.com]
>>> Sent: Friday, 5 November 2004 12:12
>>> To: ruby-talk ML
>>> Subject: Re: Cleaner way to do this?
>>>
>>> How about
>>>
>>> [1, 2, 3, 4, 5].each_with_index do |x, i|
>>> if i == 0
>>> print x
>>> else
>>> print ", ",x
>>> end
>>> end
>>
>> That's what I initially suggested :-).
>>
>> However, he felt it was a bit crazy to do the test every time, given the
>> large size of the array.
>>
>
> how about this?
>
> arr = [1, 2, 3, 4, 5]
> first = arr.shift
> print arr.inject(first) { | c, i | "#{c.to_s}, #{i.to_s}" }
>
> benny

or even

print [1, 2, 3, 4, 5].join(' ,')

benny

benny

11/5/2004 2:04:00 PM

0

benny wrote:

>
> arr = [1, 2, 3, 4, 5]
> first = arr.shift
> print arr.inject(first) { | c, i | "#{c.to_s}, #{i.to_s}" }
>
> benny

now I saw the initial post :)
sorry for the noise

benny

dblack

11/5/2004 2:07:00 PM

0