5

Array exemple

[
 [
   "Francis",
   "Chartrand",
   "[email protected]"
 ],
 [
   "Francis",
   "Chartrand",
   "[email protected]"
 ],...
]

Result wanted

"[email protected], [email protected], ..."

My solution (two loop)

array.map{|a| a[2]}.join(", ")

Is it possible to do this with one loop?

6
  • 1
    I wouldn't consider join to be a loop. Commented Oct 1, 2013 at 14:22
  • @SergioTulentsev Ahah not for a homework, just for refactoring and personal informations Commented Oct 1, 2013 at 14:26
  • 1
    Seems more like refuctoring to me. map/join is common idiom, don't stray from it. Commented Oct 1, 2013 at 14:27
  • Well. you could consider join to be a loop, when you look at the runtime (it is implemented using a O(n) loop) Commented Oct 1, 2013 at 14:27
  • 2
    I believe that map/join is better, and faster than one loop cyle with a condition check. Commented Oct 1, 2013 at 14:28

2 Answers 2

8

Using Enumerable#inject we can do the task in one loop:

a = [
  ["Francis", "Chartrand", "[email protected]"],
  ["Francis", "Chartrand", "[email protected]"]
]
a.inject(nil) {|str, arr| str ? (str << ', ' << arr[2]) : arr[2].dup}
#=> "[email protected], [email protected]"

However, this is an academic thing only, because map/join is faster and more readable anyways. See this benchmark:

             user   system    total       real
map/join 1.440000 0.000000 1.440000 ( 1.441858)
inject   2.220000 0.000000 2.220000 ( 2.234554)
Sign up to request clarification or add additional context in comments.

5 Comments

Nice answer, but map and join still faster gist.github.com/chartrandf/6779594
@FrancisChartrand Of course map and join is not only faster, but is more straightforward. And that is what everyone is telling you. But since you insist on not using map and join, and since you insist on doing it in one loop, and since you do not mention the speed in the question, this is what you get.
FrancisChartrand, sawa you both are right. map/join is faster (I have created a second benchmark where my answer is optimized for performance (so its a little more fair)). I better state that in my answer :)
@tessi Pay attention that the optimization you have done alters the original array. After executing it a[0] is ["Francis", "Chartrand", "[email protected], [email protected]"].
@toro2k good catch. Fixed it (which makes the code look a little clumsy). But we all agree to use map/join anyways, so clumsiness shouldn't really matter :)
1

Here's one approach, but it may not be especially fast.

s = ''          
array.flatten.each_slice(3) {|e| s += e.last + ', '} 
s.chop.chop

Here's another:

array.transpose[2].join(', ')

I assume you wanted a single string of email addresses for the entire array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.