Spliting hash using map!

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.

2009/10/12 Ne Scripter [email protected]:

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

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
Blog: angico.org/blog

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

== Cooperação é muito melhor que competição ==

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
Blog: angico.org/blog

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

== Cooperação é muito melhor que competição ==

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs