[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to put array into variables

Luca Scaljery

11/19/2006 12:16:00 PM

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

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

7 Answers

Vincent Fourmond

11/19/2006 12:26:00 PM

0

Luca Scaljery wrote:
> 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
--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Luca Scaljery

11/19/2006 12:30:00 PM

0

thanks a lot!!!

LuCa

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

evgeny.zislis@gmail.com

11/20/2006 5:02:00 PM

0

a,*b = [1,2,3,4,5,6,7,8,9]
a => 1
b => [2, 3, 4, 5, 6, 7, 8, 9]


On Nov 19, 2:15 pm, Luca Scaljery <lca...@gmail.com> wrote:
> 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
>
> --
> Posted viahttp://www.ruby-....

Bruno Michel

11/20/2006 5:09:00 PM

0

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

evgeny.zislis@gmail.com a écrit :
> a,*b = [1,2,3,4,5,6,7,8,9]
> a => 1
> b => [2, 3, 4, 5, 6, 7, 8, 9]
>
>
> On Nov 19, 2:15 pm, Luca Scaljery <lca...@gmail.com> wrote:
>> 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

--
Bruno Michel

Ken Allen

11/20/2006 5:55:00 PM

0

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 Fourmond wrote:
> Luca Scaljery wrote:
>
>> 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
>


wmwilson01

11/21/2006 2:32:00 AM

0

Ken Allen wrote:
> 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"

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

wmwilson01

11/21/2006 2:35:00 AM

0

El Gato wrote:
> Ken Allen wrote:
>> 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"

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