Spliting hash using map!

N

Ne Scripter

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
0001. Could anyone assist me with this query?

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

Robert Klemme

2009/10/12 Ne Scripter said:
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
 
A

angico

Hi,

=2D-----
individual items, I use map! (output =3D 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!, ju=
st=20
map. You also don't need flatten!, just flatten.

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

=2D-=20
angico
=2D-----

Site: angico.org
Blog: angico.org/blog

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

=3D=3D Coopera=C3=A7=C3=A3o =C3=A9 _muito_ melhor que competi=C3=A7=C3=A3o =
=3D=3D

=2D-----
contatos:
email: (e-mail address removed)
skype: angico00
 
N

Ne Scripter

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
 
7

7stud --

Ne said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top