From: Brian I. <in...@tt...> - 2002-08-19 17:58:23
|
On 19/08/02 09:54 -0400, Steve Howell wrote: > From: "why the lucky stiff" <yam...@wh...> > > Brian Ingerson (in...@tt...) wrote: > > 3. Iterative is currently read-only but I'd like to make it so > > you could iterate through each object and provide a means > > for providing an output stream so that any object returned > > from the block would be written to the output. In other > > words, to half all the salaries in the employee list: > > > > emplist = YAML::Stream.new( File.open( 'EMPLOYEES.yml' ) ) > > emplist2 = YAML::Stream.new( File.open( 'EMPLOYEES.yml~' ) ) > > emplist.each_output_to( emplist2 ) { |e| > > e['salary'] /= 2 > > } > > > > I think your example makes sense in Ruby, but I would do it like this in Python: > > f = open('pissed_employees.yml', 'w') > f.write('# All these employees got their salaries cut in half.\n") > for emp in yaml.fileLoader('employees.yml'): > emp['salary'] /= 2 > f.write(yaml.dump(emp)) > f.close() Or in Perl: YAML::DumpFile('pissed_employees.yml', grep $_->{salary} /= 2, YAML::LoadFile('employees.yml')); Cheers, Brian |