YAML anchor and alias problem

  • Thread starter stevegoobermanhill
  • Start date
S

stevegoobermanhill

Hi,
I'm trying to use YAML to load and store a whole bunch of data (with a
lot of repetition). Anchors and aliases seem to be the way froward,
but I am runing into a few problems.

Suppose I have the following YAML document:
---
anchor: &id001
- sample1 :
name: s1
freq: 0.27
- sample2 :
name: s2
freq: 0.27
alias: *id001

held in a string (str). I then load it

x=YAML.load(str)
=> {"anchor"=>[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil},
{"name"=>"s2", "freq"=>0.27, "sample2"=>nil}], "alias"=>nil}

Why does my alias always come out as nil. Where do I need to put the
anchor in order that the alias picks up the entire list. This is the
output I desire
=> {"anchor"=>[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil},
{"name"=>"s2", "freq"=>0.27, "sample2"=>nil}], "alias"=>
[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil}, {"name"=>"s2",
"freq"=>0.27, "sample2"=>nil}]}

Thanks

Steve
 
S

stevegoobermanhill

Hi,
I'm trying to use YAML to load and store a whole bunch of data (with a
lot of repetition). Anchors and aliases seem to be the way froward,
but I am runing into a few problems.

Suppose I have the following YAML document:
---
anchor: &id001
- sample1 :
  name: s1
  freq: 0.27
- sample2 :
  name: s2
  freq: 0.27
alias: *id001

held in a string (str). I then load it

x=YAML.load(str)
=> {"anchor"=>[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil},
{"name"=>"s2", "freq"=>0.27, "sample2"=>nil}], "alias"=>nil}

Why does my alias always come out as nil. Where do I need to put the
anchor in order that the alias picks up the entire list. This is the
output I desire
=> {"anchor"=>[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil},
{"name"=>"s2", "freq"=>0.27, "sample2"=>nil}], "alias"=>
[{"name"=>"s1", "freq"=>0.27, "sample1"=>nil}, {"name"=>"s2",
"freq"=>0.27, "sample2"=>nil}]}

Thanks

Steve

Belay that - problem found - I didn't have the indenting right

Steve
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top