The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting spec info from finger. franny UNIX for Dummies Questions & Answers 6 12-12-2007 08:45 AM
Extracting specific info from finger command franny UNIX for Dummies Questions & Answers 1 12-05-2007 01:13 PM
Extracting Info muneebr UNIX for Dummies Questions & Answers 3 09-06-2005 08:57 AM
extracting info from Unix database to construct a visual diagram fusion99 UNIX for Advanced & Expert Users 0 11-30-2004 01:29 AM
Perl - Iterating a hash through a foreach loop - unexpected results quantumechanix Shell Programming and Scripting 5 12-15-2003 07:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-04-2008
wrapster wrapster is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 193
problems iterating in RUBY while extracting info from YAML, Pls help!

Hi all,
I am stuck with a ruby script that extracts detials from yaml file and processes accordingly.
the yaml file

Code:
confivnic:
  device:
    vnic1:
      policy:
      - L2
      mode: active
    vnic2:
      policy:
      - L3
      - L4
      mode: active
  type: aggr

notsoconfi:
  device:
    vnic9:
      policy:
      - L2
      - L3
      mode: "off"
    vnic3:
      policy:
      - L2
      - L3
      mode: active
    vnic6:
      policy:
      - L3
      - L2
      mode: active
  type: aggr

the ruby script

Code:
require 'yaml'
yaml = YAML::load(File.open("testyaml.yaml"))

yaml.each do |name, option|
if option['type'] == "aggr"
names = Array.new
names.push name
while names.size != 0
key = yaml.keys.first
tpl = "dladm create-{{{type}}} {{{sets}}} {{{cmd}}}"
tpl.gsub! '{{{type}}}', yaml[key]['type']
tpl.gsub! '{{{cmd}}}',  key
sets = ''
set_tpl = '-l {{{key}}} -L={{{mode}}} -P={{{policy}}} '
yaml[key]['device'].each do |device|
  set = set_tpl.dup
  set.gsub! '{{{key}}}',    device[0]
  set.gsub! '{{{mode}}}',   device[1]['mode']
  set.gsub! '{{{policy}}}', device[1]['policy'].join(',')
  sets << set
end
names.pop
end
tpl.gsub! '{{{sets}}}', sets
puts tpl.gsub(/\s+/,' ')
end
end

The o/p i get is like this

Code:
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi

but the yaml file has 2 names ,"confivnic" and "notsoconfi"
This was written originally for just one module but for obvious reasons we had to change it. I tried all i knew but really not finding a way out.
The things that i feel are creating errors are yaml.keys.first
or the way iteration is done.

The o/p i want is similar to the shown one but instead of repeating the same one twice i want the other one to be displayed as well with the necessary changes.

Pls help
  #2 (permalink)  
Old 10-05-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,937
Yes, one of your problems is the repeated calls to yaml.keys.first. (will always return 'notsoconfi')

The following code delivers the expected output:

Code:
require 'yaml'

set_tpl = '-l {device} -L={mode} -P={policy} '

yaml = YAML::load(File.open("testyaml.yaml"))

yaml.each do |name, option|
   if option['type'] == "aggr"
      tpl = 'dladm create-{type} {sets} {cmd}'
      tpl.gsub! '{type}', option['type']
      tpl.gsub! '{cmd}', name

      sets = ""
      yaml[name]['device'].each do |device|
         set = set_tpl.dup
         set.gsub! '{device}', device[0]
         set.gsub! '{mode}',   device[1]['mode']
         set.gsub! '{policy}', device[1]['policy'].join(',')
         sets << set
      end

      tpl.gsub! '{sets}', sets
      puts tpl.gsub(/\s+/,' ')
   end
end


Code:
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi
dladm create-aggr -l vnic1 -L=active -P=L2 -l vnic2 -L=active -P=L3,L4 confivnic

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0