Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm reading big csv files and mashing the lines into hierarchical objects for analysis and display. The csv might look like this:

YEAR,QUAD,SPEC,FIASP,COND,SZ2,SZ3,SZ4,SZ5,SZ6,SZ7,SZ8,SZ9
1984,4D1,1,129,1,1,0,1,1,0,1,3,0
1984,4D1,8,371,0,0,0,0,0,1,0,0,1
1984,4D1,9,375,1,0,0,0,0,0,0,1,0
1984,4D1,1,129,0,2,0,1,1,1,1,1,0
1984,4D1,3,012,0,0,1,0,3,1,0,0,0
...
2009,8I3,5,833,1,0,0,0,0,0,0,1,0
2009,8I3,7,316,1,0,0,0,0,0,0,0,1
2009,8I3,25,585,0,1,1,2,1,0,0,0,0
2009,8I3,7,316,0,0,0,0,0,1,1,0,1

My non-clever reading code works but I dislike it; it goes like this (could be typos from editing down):

function makePlotData() {
if(ground.reader.readyState==4) {
    var lines = ground.reader.responseText.split(/\r\n?/);
    for (var i=0; i<lines.length; i++) {
        if (lines[i].match(/^\d\d\d\d,/)) {     // then it's a regular csv line
            var fieldsArr = lines[i].split(',');
            var [year, quad, spec] = [fieldsArr[0],fieldsArr[1],fieldsArr[2]];
            ground[quad] = ground[quad] || {};
            ground[quad][year] = ground[quad][year] || {};
            ground[quad][year][spec] = ground[quad][year][spec] || {};
            ground[quad][year][spec].sum = ground[quad][year][spec].sum || 0;
            ground[quad][year][spec].sizes = ground[quad][year][spec].sizes || [];
            var sizeArr = fieldsArr.slice(5).map(Number);
            if (ground[quad][year][spec].sizes.length) {  // already have a size classes Arr
                for(var s=0;s<sizeArr.length;s++){  // ...so add to it
                    ground[quad][year][spec].sizes[s] += sizeArr[s];}
            } else {ground[quad][year][spec].sizes = sizeArr;}
        } // ... continue with assignments and doing stuff

My objection is to that series of statements like ground[quad] = ground[quad] || {}; , which do what I need but seem like a lot of unnecessary assigning and reassigning; I bet it doesn't matter but it gives me a feeling of wasted cycles and room for errors; it's also ungainly and a nuisance to modify. Is there any idiom or anything that manages this more efficiently or is this as good as it gets with this kind of structure?

share|improve this question
    
@200_success Hi 200 -- is that title edit according to a site stylebook or something? It is indeed a neater phrase, but I find the googling results are a lot more on-point when the titles include the language in question. – uhClem Jul 14 at 16:41
    
Yes, that is the site standard— also see How to Ask. – 200_success Jul 14 at 16:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.