How to put array into variables

L

Luca Scaljery

Hi All

I tried to put an array into variables like this

s, h = b.scan(/^(\w+)\s(\w+)$/)

But for some reason 's' becomes an array and 'h' is undefined!!

Any suggestions how to put the first result in 's' and de second in 'h'
?

Thnx a lot
LuCa
 
V

Vincent Fourmond

Luca said:
Hi All

I tried to put an array into variables like this

b = "aaa bbb"
s, h = b.scan(/^(\w+)\s(\w+)$/)

But for some reason 's' becomes an array and 'h' is undefined!!

The problem is that scan returns an array of arrays:
[ # first match:
[ #first group :
"aaa",
# second group:
"bbb"
]
]

So you might want in your case:

s, h = b.scan(/^(\w+)\s(\w+)$/)[0]
s, h = b.scan(/^(\w+)\s(\w+)$/).flatten

Vince
 
B

Bruno Michel

a, b = *[1, 2, 3]
a => 1
b => 2
3 has been rejected.

(e-mail address removed) a écrit :
a,*b = [1,2,3,4,5,6,7,8,9]
a => 1
b => [2, 3, 4, 5, 6, 7, 8, 9]


Hi All

I tried to put an array into variables like this

s, h = b.scan(/^(\w+)\s(\w+)$/)

But for some reason 's' becomes an array and 'h' is undefined!!

Any suggestions how to put the first result in 's' and de second in 'h'
?

Thnx a lot
LuCa
 
K

Ken Allen

This also works:
b = "word1 word2"
((a,b)) = b.scan(/^(\w+)\s(\w+)$/)
p a => "word1"
p b => "word2"

The outer parens matches the outer array, and the inner parens matches
the inner array, allowing a and b to decompose it.
It's a pretty useful trick when decomposing nested arrays.

Ken

Vincent said:
Luca said:
Hi All

I tried to put an array into variables like this

b = "aaa bbb"
s, h = b.scan(/^(\w+)\s(\w+)$/)

But for some reason 's' becomes an array and 'h' is undefined!!

The problem is that scan returns an array of arrays:
[ # first match:
[ #first group :
"aaa",
# second group:
"bbb"
]
]

So you might want in your case:

s, h = b.scan(/^(\w+)\s(\w+)$/)[0]
s, h = b.scan(/^(\w+)\s(\w+)$/).flatten

Vince
 
E

El Gato

Ken said:
This also works:
b = "word1 word2"
((a,b)) = b.scan(/^(\w+)\s(\w+)$/)
p a => "word1"
p b => "word2"

The outer parens matches the outer array, and the inner parens matches
the inner array, allowing a and b to decompose it.
It's a pretty useful trick when decomposing nested arrays.

Ken

Seems like the long way around...

foo = ["one", "two"]
a, b = foo
a #=> "one"
b #=> "two"
 
E

El Gato

El said:
Ken said:
This also works:
b = "word1 word2"
((a,b)) = b.scan(/^(\w+)\s(\w+)$/)
p a => "word1"
p b => "word2"

The outer parens matches the outer array, and the inner parens matches
the inner array, allowing a and b to decompose it.
It's a pretty useful trick when decomposing nested arrays.

Ken

Seems like the long way around...

foo = ["one", "two"]
a, b = foo
a #=> "one"
b #=> "two"

Actually, to be more clear

foo = "hello world"
a,b = foo.split
a #=> "hello"
b #=> "world"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top