Forum: Ruby Array matching question

0a49c53f8f4a038037ca841458dddbad?d=identicon&s=25 Markis Carrier (markisc)
on 2017-01-25 18:30
Hi,

What I am trying to achieve is a .match within an array.

array = {Danby:"347", Jerry:"037, Mary:"221"}

#I want to get the numeric value if the text value is found in the
array.

if(/#input/.match(array) #this doesn't work.



I know it's something simple, I just can't seem to find it yet.

Thanks in advance.
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2017-01-26 12:10
Markis Carrier wrote in post #1185489:
> Hi,
>
> What I am trying to achieve is a .match within an array.
>
> array = {Danby:"347", Jerry:"037, Mary:"221"}
>

That's a hash, which is not an array. A " is missing too ...

> #I want to get the numeric value if the text value is found in the
> array.
>
> if(/#input/.match(array) #this doesn't work.
>

What text value? "#input" doesn't appear anywhere in your data
structure.
A ) is missing too ...

>
>
> I know it's something simple, I just can't seem to find it yet.
>
> Thanks in advance.

Are you struggling with an XY problem?

"""
You want to do X, and you think Y is the best way of doing so. Instead
of asking about X, you ask about Y.
"""
0a49c53f8f4a038037ca841458dddbad?d=identicon&s=25 Markis Carrier (markisc)
on 2017-01-31 02:15
Thanks for the reply Bonzo,

I guess it may be a little late to mention that I am new to Ruby....
My apologizes for the mistakes, I posted with my phone and didn't have
the actual code with me.

As for the XY problem... You may be right.

I would like certain keywords to have a numeric value.
When a keyword is found, then the numeric value is returned.

Thanks!
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2017-01-31 12:26
Markis Carrier wrote in post #1185503:
> Thanks for the reply Bonzo,
>
> I guess it may be a little late to mention that I am new to Ruby....
> My apologizes for the mistakes, I posted with my phone and didn't have
> the actual code with me.
>
> As for the XY problem... You may be right.
>
> I would like certain keywords to have a numeric value.
> When a keyword is found, then the numeric value is returned.
>
> Thanks!

Hash might help

"""
A Hash is a dictionary-like collection of unique keys and their values.
Also called associative arrays, they are similar to Arrays, but where an
Array uses integers as its index, a Hash allows you to use any object
type.
"""

http://ruby-doc.org/core-2.1.8/Hash.html
(or navigate to the appropriate one for your ruby version)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.