SlightlyLoony
Tera Contributor
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
10-03-2011
11:01 AM
Yesterday, Snorklebogger (our developer at right) was working a tough problem: his code had a pair of nested loops, and he needed to handle a condition detected in the inner loop by skipping to the next iteration of the outer loop. How can he do this?
Take a look at the code he started with, and ponder his problem:
// some test data...
var input = 'jane:gender=female;age=34;skills=ok;hair=blond\n' +
'jim:gender=male;age=?;skills=awful;hair=brown\n' +
'bosun:gender=male;age=99;skills=good;hair=none';
var parsed = parse(input);
JSUtil.logObject(parsed, 'parsed');
function parse(input) {
var result = {};
// iterate over the people in the input string...
var people = input.split('\n');
for (var p = 0; p < people.length; p++) {
var personParts = people[p].split(':');
var person = {};
var name = personParts[0];
var attrStr = personParts[1];
result[name] = person;
person.name = name;
// iterate over the attributes for this person...
var attrs = attrStr.split(';');
for (var a = 0; a < attrs.length; a++) {
var attrParts = attrs[a].split('=');
var attrName = attrParts[0];
var attrValue = attrParts[1]; // here's where we need to detect a problem...
person[attrName] = attrValue;
}
}
return result;
}
When he ran this code, he got this result:
Log Object: parsed
Object
jim: Object
gender: string = male
name: string = jim
age: string = ?
skills: string = awful
hair: string = brown
bosun: Object
gender: string = male
name: string = bosun
age: string = 99
skills: string = good
hair: string = none
jane: Object
gender: string = female
name: string = jane
age: string = 34
skills: string = ok
hair: string = blond
This was almost correct, but had one pesky problem: the attrValue could have an inappropriate value assigned to it (see the commented line near the end of the code above). When that happens, Snorklebogger wanted to log the error and refrain from including that person in the result. Without twisting the code into salt-free pretzels, how could he do this? After several cups of coffee and a hamburger of questionable ancestry, Snorklebogger started Googling in sheer desperation — and found out about labeled continue and break statements. They were exactly what he needed — and armed with his new and profound knowledge he was able to craft a solution:
// some test data...
var input = 'jane:gender=female;age=34;skills=ok;hair=blond\n' +
'jim:gender=male;age=?;skills=awful;hair=brown\n' +
'bosun:gender=male;age=99;skills=good;hair=none';
var parsed = parse(input);
JSUtil.logObject(parsed, 'parsed');
function parse(input) {
var result = {};
// iterate over the people in the input string...
var people = input.split('\n');
outer: // the label marking the point we want to continue to...
for (var p = 0; p < people.length; p++) {
var personParts = people[p].split(':');
var person = {};
var name = personParts[0];
var attrStr = personParts[1];
person.name = name;
// iterate over the attributes for this person...
var attrs = attrStr.split(';');
for (var a = 0; a < attrs.length; a++) {
var attrParts = attrs[a].split('=');
var attrName = attrParts[0];
var attrValue = attrParts[1];
if (!attrValue || (attrValue == '?')) {
gs.log('Person ' + name + ' has data errors...');
continue outer; // continue goes to the label, not the closest loop...
}
person[attrName] = attrValue;
}
result[name] = person;
}
return result;
}
When our easily-amazed fellow ran his new code, he saw this:
Person jim has data errors...
Log Object: parsed
Object
bosun: Object
gender: string = male
name: string = bosun
age: string = 99
skills: string = good
hair: string = none
jane: Object
gender: string = female
name: string = jane
age: string = 34
skills: string = ok
hair: string = blond
Oddly enough, that's exactly what he wanted.
This little trick falls squarely into the category of "Things you're not likely to need, but when you do, oh my is it a handy thing to know!" Keep in your back pocket for a desperate day...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.