Add value inside 'Nested Mappings String'

K

Kamarulnizam Rahim

Hi,

Im trying to add some data/value into my nested mapping file(strings)
output but its not working. Below the example of my output:

{"System"=>
{"H&S"=>
{"name"=>"Health & Safety",
"components"=>["FlashWheel"],
"children"=>
[{"name"=>"Training and Culture",
"type"=>"Programme",
"subtype"=>"None",
"components"=>..................

For example how can i change the children name from "Training and
Culture" to let say "Objective"? Do i need to build a new hash for this
data and modify that hash? Thanks in advance.

Nizam
 
A

Abinoam Jr.

Perhaps you're not considering that "children" returns an Array!
The Hash inside it is the 0 indexed element of this Array.

a =3D {"System"=3D>
=A0{"H&S"=3D>
=A0 =A0{"name"=3D>"Health & Safety",
=A0 =A0 "components"=3D>["FlashWheel"],
=A0 =A0 "children"=3D>
=A0 =A0 =A0[{"name"=3D>"Training and Culture",
=A0 =A0 =A0 =A0"type"=3D>"Programme",
=A0 =A0 =A0 =A0"subtype"=3D>"None",
=A0 =A0 =A0 =A0"components"=3D>"None"}]}}}

a["System"]["H&S"]["children"][0]["name"]=3D"Objective"

now 'a' returns

{"System"=3D>{"H&S"=3D>{"name"=3D>"Health & Safety",
"components"=3D>["FlashWheel"], "children"=3D>[{"name"=3D>"Objective",
"components"=3D>"None", "subtype"=3D>"None", "type"=3D>"Programme"}]}}}

Abinoam Jr.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Hi,

Im trying to add some data/value into my nested mapping file(strings)
output but its not working. Below the example of my output:

{"System"=>
{"H&S"=>
{"name"=>"Health & Safety",
"components"=>["FlashWheel"],
"children"=>
[{"name"=>"Training and Culture",
"type"=>"Programme",
"subtype"=>"None",
"components"=>..................

For example how can i change the children name from "Training and
Culture" to let say "Objective"? Do i need to build a new hash for this
data and modify that hash? Thanks in advance.

Nizam
I think working with this data will quickly become very difficult to deal
with. For an example, see the latest Peepcode (
http://peepcode.com/products/play-by-play-bernhardt) where they spend half
the screencast trying to figure out what their data looks like (its all
nested Hashes and Arrays, like yours). I think you should turn your data
into instances of some class[es] that defines how to deal with these
relationships in in a humane way.
 
K

Kamarulnizam Rahim

Hi Abinoam Jr,

How do you know the hash inside it is indexed 0? This is because the
rest of the outputs consist of nearly 4000 lines. Thus, locating
contents wil be very difficult if i dnt know how to find it. Thanks

Nizam
 
A

Abinoam Jr.

Well, Arrays are ordered by nature. (Hashes shouldn't be considered so).
That particular Hash was the first element of an Array.
So, it was the "0" element.

Look.

var = [ "first", 2, { :third => 3, :not_third => true}]

var[0] #=> "first" (a String)
var[1] #=> 2 (a Fixnum)
var[2] #=> {:third=>3, :not_third=>nil} (a Hash)
var[2][:third] #=> 3 (The :third key of that Hash is pointing to the Fixnum 3)
var[2][:not_third] #=> true

4000 lines?!

I strongly agree with Josh.
You should use another kind of data representation.
If you have no choice with the input data, you can (recursively) parse
it and transform it into something human readable.

Abinoam Jr.
 
K

Kamarulnizam Rahim

Hi Josh and Abinoam Jr,

Actually, those contents will be converted into YAML format for other
purpose where indentation is very important. And i dont think other kind
of data representation is suitable for that purpose. Anyway, what do you
mean by 'recursively parse' it? And where can i find any tutorials or
examples that use 'that', if by any chance available on the net?

Thanks in advance

Nizam
 
K

Kamarulnizam Rahim

Hi Abinoam,

I tried to change the children type from "programme" to "objective" by
using the following code:

a["System"]["H&S"]["children"][1]["type"]="Objective"

but error came out. I used '1' as i thought that hash is the second
element of an array (based on your previous post). Thanks

Nizam
 
A

Abinoam Jr.

In your example, there's only one element in the Array, and 4 elements
in the inner Hash.

Step-by-step...

1) Returns an Array.

a["System"]["H&S"]["children"]

#=> [{"name"=>"Training and Culture", "type"=>"Objective",
"subtype"=>"None", "components"=>"None"}]

2) Returns the first element of the Array. It's the Hash you're
trying to manipulate.

a["System"]["H&S"]["children"][0]

# => {"name"=>"Training and Culture", "type"=>"Objective",
"subtype"=>"None", "components"=>"None"}

3) Now that you have the Hash, you can access its pairs (key,value).

a["System"]["H&S"]["children"][0]["type"]
#=> "Programme"

4) Or, set it...

a["System"]["H&S"]["children"][0]["type"] = "Objective"

Abinoam Jr.
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top