Seth Haberman
Seth Haberman

Reputation: 141

Haxe Iteration of defining variables

I have a chunk of code that is parsing xml into variables. On one of the variables, there will never be more than 12 but there may be less. I am trying to define each of these variables multiple times a second. However I only need them defined if they have a variable because right now, if there is less than 12 variables, the program crashes. Right now this code below works, but only if there is 12 values present in my xml document. How do I itterate this code so I don't have a list of "if != null" statements?

var _vol1 = (ipts[0].volume);
var _vol2 = (ipts[1].volume);
....
....
var _vol12 = (ipts[11].volume);

This code works when 12 values are in my xml document but crashes if there is less than 12 which sometimes might be the case.

How do I structure the code to define/redefine the variable only if there is a value and is not null? This what what needs to be iterated????

If ((ipts[0].volume) != null ){
    var _vol1 = (ipts[0].volume);
}

I researched on the Haxe website but wasnt sure where this is applicable. My syntax and programming is not the greatest. Thanks for the help. Sorry if this is a bad question.

UPDATE: Here is my complete code as of now. It doesn't crash but its not defining the volume variables anymore now that I added the if statements

var xml = Xml.parse(_vMixData);

// wrap the xml for fast access
var data = new haxe.xml.Fast(xml.firstElement());

//Getting the data from inputs (here we are getting the xml node 'inputs', which contain two 'input' nodes, as per your sample xml file.
var inputs = data.node.inputs;
var ipts = new Array<VInput>();

for (input in inputs.nodes.input) {
    var ipt = new VInput();
    if (input.has.state) { 
       ipt.state = input.att.state;
       ipt.value = input.innerHTML; 
           }
    if (input.has.volume) {
        ipt.volume = Std.parseInt(input.att.volume);
          }
    if (input.has.muted) {
        ipt.muted = Std.string(input.att.muted);
          }
    ipt.value = input.innerHTML;          
    ipts.push(ipt);
}

var overlays = data.node.overlays;
var ovlys = new Array<VOverlay>();
for (overlay in overlays.nodes.overlay) {
    var ovly = new VOverlay();
    if (overlay.has.number) { 
        ovly.number = Std.parseInt(overlay.att.number);
    }
    ovly.value = overlay.innerHTML;
    ovlys.push(ovly);
}

//Defines variables based on returned information from vMix
var _fadeToBlack:Bool = data.node.fadeToBlack.innerHTML == "True" ? true : false;

var _version = data.node.version.innerHTML;
var _record:Bool = data.node.recording.innerHTML == "True" ? true : false;
var _external:Bool = data.node.external.innerHTML == "True" ? true : false;
var _stream:Bool = data.node.streaming.innerHTML == "True" ? true : false;
var _active:Int = Std.parseInt(data.node.active.innerHTML);
var _preview:Int = Std.parseInt(data.node.preview.innerHTML);

var _overlay1 = (ovlys[0].value);
var _overlay2 = (ovlys[1].value);
var _overlay3 = (ovlys[2].value);
var _overlay4 = (ovlys[3].value);
var _input1state = (ipts[0].state);


if (ipts[0].volume != null ){
var _vol1 = (ipts[0].volume);
}
if (ipts[1].volume != null ){
var _vol2 = (ipts[1].volume);
}
if (ipts[2].volume != null ){
var _vol3 = (ipts[2].volume);
}
if (ipts[3].volume != null ){
var _vol4 = (ipts[3].volume);
}
if (ipts[4].volume != null ){
var _vol5 = (ipts[4].volume);
}
if (ipts[5].volume != null ){
var _vol6 = (ipts[5].volume);
}
if (ipts[6].volume != null ){
var _vol7 = (ipts[6].volume);
}
if (ipts[7].volume != null ){
var _vol8 = (ipts[7].volume);
}
if (ipts[8].volume != null ){
var _vol9 = (ipts[8].volume);
}
if (ipts[9].volume != null ){
var _vol10 = (ipts[9].volume);
}
if (ipts[10].volume != null ){
var _vol11 = (ipts[10].volume);
}
if (ipts[11].volume != null ){
var _vol12 = (ipts[11].volume);
}

//var _1mute = (ipts[0].muted);
//var _2mute = (ipts[1].muted);
//var _3mute = (ipts[2].muted);
//var _4mute = (ipts[3].muted);
//var _5mute = (ipts[4].muted);
//var _6mute = (ipts[5].muted);
//var _7mute = (ipts[6].muted);
//var _8mute = (ipts[7].muted);
//var _9mute = (ipts[8].muted);
//var _10mute = (ipts[9].muted);
//var _11mute = (ipts[10].muted);
//var _12mute = (ipts[11].muted);



if (ipts[2] != null){
    var _ovly3 = 1;
}
//trace(ovlys[2].value);
//trace(ovlys[0].value);
//trace(ipts[0].state);
//trace(_active);
//trace(_preview);
//trace(_fadeToBlack);

Upvotes: 1

Views: 110

Answers (1)

MSGhero
MSGhero

Reputation: 411

To look at each thing in an XML, you do for (child in xml). To have any number of variables, you need to use an array, not var1 through varInfinity.

You didn't post enough code, so I'm guessing here:

var varArray = [];
for (child in xml) {
    varArray.push(child.volume);
}

That way, it doesn't matter how many variables you have, the code adapts (rather than you adapting by adding var1, var2, etc).

Upvotes: 1

Related Questions