Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am writing a program that will deal with formatting the text in a text area.

The user will use Ctrl + A, Ctrl + C to copy the entirety of a web page, then paste that text into a text area. The program will take the text area, remove the junk, and output the important information to then be copied and pasted into a different system.

The concept is simple, but based on my education in programming I need a little help with the pseudocode to make sure I write this correctly and efficiently. I know this website is about code review and refactoring, so I've waited to post until I had some code written. I have already spent about three weeks on this project and I am moving too slowly. Any help would be greatly appreciated.

Here's what I know (or think I know) needs to be done: - The program will use a loop to pass through the text box contents one time, extracting the important information and putting it into the appropriate variables - There will be a two dimensional array, or rather, an array of objects. The objects will be the account lines, each with information like account number, device type, phone number, etc. - The system needs to be able to identify certain fields that can be blank. I will give an example:

Text that will be in the text box:

Account Number - Account Name - Product - Phone - Extension - Equipment - Status - Start Date - Order ID

ER4402 - testaccount - Unlimited Product - 1234567890 - 987 - FRZ142 - Active - 12-12-12 - ABC12345 ER4403 - testaccount2 - Unlimited Product - - - - Active - 12-12-12 - ABC12345

Notice the second has empty fields. I think they would be /t in the text box.

Edit: I pressed enter too soon... writing more info now

Here are the objects I've made

// Customer "class"
var customer = {
    name: "",
    phone: "",
    compName: "",
    email: "",
    getInfo: function () {
        return "Details: " + this.name + ", " + this.phone + ", " + this.compName + ", " + this.email;
    }
}

// Account lines "class"
var account = {
    checked: 0,
    number: "",
    ipbx: "",
    product: "",
    phoneNumb: "",
    ext: "",
    equip: "",
    status: "",
    startDate: "",
    order: "",
    getInfo: function () {
        return "Details: " + this.number + ", " + this.ipbx + ", " + this.product + ", " + this.phoneNumb + ", " + this.ext + ", " + this.equip + ", " + this.status + ", " + this.startDate + ", " + this.order;
    }
}

The code:

// Parse the input box into an array
var inputArr = document.getElementById("inputBox").value.split(/[ ]/);

var contNameBool = new Boolean();
var compNameBool = new Boolean();
var emailBool = new Boolean();
var accountsBool = new Boolean();
var accountLinesArray = new Array();
var marker = 1;
var arrayNumb = 0;
for(i = 0; i < inputArr.length; i++) {
    switch(inputArr[i]) {
        case "Name":
            if(inputArr[i - 1] == "Summary") {
                contNameBool = true;
                break;
            }
            break;
        case "Details":
            if(contNameBool == true) {
                contNameBool = false;
                break;
            } else if(contNameBool == false && compNameBool == true) {
                compNameBool = false;
                break;
            }
            break;
        case "Customer":
            if(inputArr[i - 1] == "Details") {
                contNameBool = false;
                compNameBool = true;
                break;
            }
            break;
        case "Address":
            if(inputArr[i - 1] == "Profile") {
                compNameBool = false;
                emailBool = true;
                break;
            }
            break;
        case ("VISA" || "MASTERCARD" || "AMERICAN" || "DISCOVER"):
            emailBool = false;
            break;
        case "Show":
            if(inputArr[i + 1] == "next" && inputArr[1 + 2] == "row") {
                accountLinesArray.length = custDet[i - 1];
                break;
            }
            break;
        case "Order":
            if(inputArr[i + 1] == "ID" && inputArr[i + 2] == "MRC") {
                accountsBool = true;
                break;
            }
            break;
    }

    if(contNameBool == true && inputArr[i] != "Name") {
        if(inputArr[i].search(",") != -1) {
            var temp = inputArr[i].split(/[,]/);
            temp.reverse();
            if(customer.name == "") {
                customer.name = temp.join(" ");
            } else {
                customer.name = customer.name + " " + temp.join(" ");
            }
        } else {
            if(customer.name == "") {
                customer.name = inputArr[i];
            } else {
                customer.name = customer.name + " " + inputArr[i];
            }
        }
    } else if(compNameBool == true) {
        if (customer.compName == "") {
            customer.compName = inputArr[i + 1] + " ";
        } else if(inputArr[i + 1] == "Details") {
            void(0);
        } else {
            customer.compName = customer.compName + inputArr[i + 1] + " ";
        }
    } else if(emailBool == true) {
        if(customer.email == "") {
            customer.email = inputArr[i + 1];
        } else if(inputArr[i + 1] == "VISA" || "MASTERCARD" || "AMERICAN" || "DISCOVER") {
            void(0);
        } else {
            customer.email = customer.email + " " + inputArr[i + 1];

        }
    } else if(accountsBool == true) {
        if(inputArr[i].replace(/[\t]/))
        switch(marker) {
            case 1:
                accountLinesArray[arrayNumb] = new account;
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
        }
        if (marker == 9) {
            marker = 1;
        } else {
            marker++;
        }
    }
}

/*

customer.name = prompt("Error: Could not locate a valid Contact Name. Please input Contact Name.");
customer.compName = prompt("Error: Could not locate a valid Company Name. Please input Company Name.");
customer.email = prompt("Error: Could not locate a valid Email Address. Please input Email Address.");

*/

document.getElementById("contactNameOutput").innerHTML = customer.name;
// document.getElementById("contactNameOutput").innerHTML = inputArr;
document.getElementById("companyNameOutput").innerHTML = customer.compName;
document.getElementById("emailAddressOutput").innerHTML = customer.email;

}

The problem I'm having is figuring out how to get the program to identify when one account line has ended and a new one has begun.

I've made a marker to have the program go back to the first part of the array, but I can't wrap my head around how to get it to identify if a field is blank and how to move on to the next field and reach the end of the account line in the right order.

I realize I just asked a lot, but I don't know where to go or how to proceed with this. I admit that it's a bit over my head, but due to my frustration and level of completion, I really thought I should post it before I put the project on a hiatus. Any feedback or help would be appreciated.

Thank you.

share|improve this question

closed as off topic by svick, Glenn Rogers, Brian Reichle, Yuushi, Jeff Vanzella Feb 4 '13 at 16:51

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
I'm sorry, but Code Review is for improving code that works, not how to fix program so that it works. For more information, see the FAQ. –  svick Feb 2 '13 at 16:59
    
I see. I guess I was a bit hasty. I'm sorry. –  Mrow Feb 2 '13 at 17:20
add comment

1 Answer 1

up vote 0 down vote accepted

I have done this a lot in php with preg_match_all(). But this should also work in javascript.

My first blind guess for a regular expression: (Having some real samples would make this easier ..., furthermore I assume that the dashes are only for the layout and not in the data, and I wrap every part for readability .)

/([A-Z]{2}\d{4})\s*
([a-z0-9]+)\s*
(.*?)\s*
(\d+)?\s*
(\d{3})?\s*
([A-Z]{3}\d{3})?\s*
(Active|foo|bar)\s*
(\d\d-\d\d-\d\d)\s*
([A-Z]{3}\d{5})/

If you are not familiar with regular expression:

  • \s* is any white space
  • \d are numbers
  • {2} is an exact count of 2,
  • + is one or more characters
  • - is none or more characters
  • )? is optional
  • ?) is a lower greedyness

For more details see: regular expression

share|improve this answer
    
Thank you. I will read more on regular expressions. I will post again when I have code that works. –  Mrow Feb 2 '13 at 17:21
    
@Mrow Upvote and except the answer before the topic get closed :P –  mnhg Feb 2 '13 at 17:25
    
I can't upvote. But I will accept the asnwer. Thank you. :) –  Mrow Feb 2 '13 at 18:30
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.