# Spliting hash using map!

**URL:** <https://www.ruby-forum.com/t/spliting-hash-using-map/177142>\
**Category:** Ruby\
**Created:** [October 12, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142 "2009-10-12T12:14:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/1 "2009-10-12T12:14:54Z")

</div>

Hello,

I have a program which goes through a number of entries, a typical  
example of these entries is as follows:

qwerty 0001  
abcdef 0002 0003 0004  
uvwxyz 0005 0006

Basically, the first string can match to one or more unique numbers.  
This structure is a hash. Later in my code I remove the string leaving  
only the numbers, I then wish to break the numbers down into their  
individual items, I use map! (output = ID.map!{|str| str.split(/\s+/)  
}.flatten!) to do this giving

0001  
000200030004  
00050006

What I want to do at this stage is have them broken into individual  
items instead of a full string for each, the purpose of this is so I can  
do something like this:

if 0001.match(/#{output}/)  
puts output  
end

So in a loop for the second entry we would be doing  
if 0001.match(/#{0002}/)…  
if 0001.match(/#{0003}/) and so on

At this stage I am not getting that result because I do not have 0002,  
0003 and so on, I only have individual entries from the hash such as

1. Could anyone assist me with this query?

Sorry if it is unclear, and may thanks for your help.

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/2 "2009-10-12T13:11:04Z")

</div>

2009/10/12 Ne Scripter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Sorry if it is unclear, and may thanks for your help.

I am at a loss here. Can you provide a more complete example which  
demonstrates what you’re after?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/3 "2009-10-12T13:19:16Z")

</div>

Hi,

* * *

On Monday 12 October 2009 07:14:57 Ne Scripter wrote:

> individual items, I use map! (output = ID.map!{|str| str.split(/\s+/)  
> }.flatten!) to do this giving
> 
> 0001  
> 000200030004  
> 00050006

If you are to put the result of map in ‘output’ the you don’t need map!,  
just  
map. You also don’t need flatten!, just flatten.

And, BTW, where does ID come from? What is ID, an array, a string?

## – angico

Site: [angico.org](http://angico.org)  
Blog: [angico.org/blog](http://angico.org/blog)

Gnu/Linux, FLOSS, Espiritismo, e eu por mim mesmo 8^I

== CooperaÃ§Ã£o Ã© _muito_ melhor que competiÃ§Ã£o ==

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/4 "2009-10-12T15:18:21Z")

</div>

Sorry for being unclear.

Basically, if I say the following

string = “0002 0003 0004 0005”

if 0002.match(/#{string}/)  
puts string  
end

So I want to say if my string contains the item given in the IF  
statement tell me the item that it found, in the example above 0002.

The code I have above does not match anything.

Thanks

angico wrote:

> Hi,
> 
> * * *
> 
> On Monday 12 October 2009 07:14:57 Ne Scripter wrote:
> 
> > individual items, I use map! (output = ID.map!{|str| str.split(/\s+/)  
> > }.flatten!) to do this giving
> > 
> > 0001  
> > 000200030004  
> > 00050006
> 
> If you are to put the result of map in ‘output’ the you don’t need map!,  
> just  
> map. You also don’t need flatten!, just flatten.
> 
> And, BTW, where does ID come from? What is ID, an array, a string?
> 
> ## – angico
> 
> Site: [angico.org](http://angico.org)  
> Blog: [angico.org/blog](http://angico.org/blog)
> 
> Gnu/Linux, FLOSS, Espiritismo, e eu por mim mesmo 8^I
> 
> == CooperaÃ§Ã£o Ã© _muito_ melhor que competiÃ§Ã£o ==

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/5 "2009-10-12T15:37:40Z")

</div>

Solved. I went for string.include?(variable)

Thanks

> angico wrote:
> 
> > Hi,
> > 
> > * * *
> > 
> > On Monday 12 October 2009 07:14:57 Ne Scripter wrote:
> > 
> > > individual items, I use map! (output = ID.map!{|str| str.split(/\s+/)  
> > > }.flatten!) to do this giving  
> > > 0001  
> > > 000200030004  
> > > 00050006  
> > > If you are to put the result of map in ‘output’ the you don’t need map!,  
> > > just  
> > > map. You also don’t need flatten!, just flatten.
> > 
> > And, BTW, where does ID come from? What is ID, an array, a string?
> > 
> > ## – angico

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [October 12, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/spliting-hash-using-map/177142/6 "2009-10-12T15:48:31Z")

</div>

Ne Scripter wrote:

> Sorry for being unclear.
> 
> Basically, if I say the following
> 
> string = “0002 0003 0004 0005”
> 
> if 0002.match(/#{string}/)  
> puts string  
> end
> 
> So I want to say if my string contains the item given in the IF  
> statement tell me the item that it found, in the example above 0002.
> 
> The code I have above does not match anything.

Your regex is /0002 0003 0004 0005/. That pattern does not appear in  
the string “0002” (and the integer 0002 does not have a match()  
method!).

Look at this example:

string = “0002 0003 0004 0005”

if string.match(/0002/)  
puts string  
end

–output:–  
0002 0003 0004 0005

In this example, the regex is /0002/, and that patter does occur in the  
string “0002 0003 0004 0005”, so there is a match.
