9

I want to split an array into three variables; the first value into one variable, the second one into another variable, and all the rest into one string, for example:

arr = ["a1","b2","c3","d4","e5","f6"]
var1 = arr[0] # var1 => "a1"
var2 = arr[1] # var2 => "b2"
var3 = ? # var3 should be => "c3d4e5f6"

What code is needed to achieve the listed values for each variable?

BenjiWiebe
  • 2,116
  • 5
  • 22
  • 41

3 Answers3

19

This seems as good as anything:

arr = ["a1","b2","c3","d4","e5","f6"]
var1 = arr[0]            # => "a1"
var2 = arr[1]            # => "b2"
var3 = arr[2..-1].join   # => "c3d4e5f6"

If you don't need to preserve arr, you could do:

arr = ["a1","b2","c3","d4","e5","f6"]
var1 = arr.shift   # => "a1"
var2 = arr.shift   # => "b2"
var3 = arr.join    # => "c3d4e5f6"

Others are pointing out the splat operator, which is understandable, but I think this is worse than the above:

arr = ["a1","b2","c3","d4","e5","f6"]
var1, var2, *tmp = arr
var3 = tmp.join

As is this:

arr = ["a1","b2","c3","d4","e5","f6"]
var1, var2, *var3 = arr
var3 = var3.join

Still, it's an option to be aware of.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
6

Here is an alternative form that uses splat assignment (aka array destructuring):

arr = ["a1","b2","c3","d4","e5","f6"]
# "splat assignment"
var1, var2, *var3 = arr
# note that var3 is an Array:
#  var1 -> "a1"
#  var2 -> "b2"
#  var3 -> ["c3","d4","e5","f6"]

See also:

Community
  • 1
  • 1
user2246674
  • 7,621
  • 25
  • 28
  • I understand wanting to use the splat operator, but you haven't actually given the OP what they asked for. Getting `var3` into the correct state (a string) loses much of the value of splatting, in my opinion. – Darshan Rivka Whittle Jun 05 '13 at 01:09
  • @DarshanComputing The OP's question is contradictory The OP is asking for an array in the question, but is giving a string in the expected output. What is to be blamed is the question, not this answer. – sawa Jun 05 '13 at 02:16
  • @sawa I've read the question three times (including the edits), and I can't see anywhere where they ask for an array. The question and the examples are 100% consistent. All three variables should be set to strings. There's no ambiguity about this. – Darshan Rivka Whittle Jun 05 '13 at 02:25
  • @DarshanComputing The input was an array, and the question was asking (in an earlier version) to take the first, second, and the remaining element(s) as the first, second, and third variables respectively, without any mentioning of joining or turning it into a string. If you take all but two elements from an array, you get an array. The first and the second elements are string from the beginning. – sawa Jun 05 '13 at 02:36
4

Use the splat operator:

arr = ["a1","b2","c3","d4","e5","f6"]
var1, var2, *var3 = arr

# var1 => "a1"
# var2 => "a2"
# var3 => ["c3", "d4", "e5", "f6"]
Cesar Figueroa
  • 214
  • 2
  • 9
  • I have the save feedback for you as I had for user2246674: this gets you largely there, but you're not done, and getting the rest of the way takes away much of the elegance of splatting, in my opinion. I'd love to be shown something elegant I haven't thought of, if it's out there. In any case, this doesn't answer the question. – Darshan Rivka Whittle Jun 05 '13 at 01:12