2

Here's the problem:

I have an array that has information about users. Each entry is separated by a blank field in the array.

For this example let's say the data fields are ID, first, last, phone, and email. However, if a user doesn't have a value for a particular field, then it is omitted entirely.

So here is what the array looks like

users[0] = 45049345
users[1] = Bob
users[2] = Smith 
users[3] = 789-456-1230
users[4] = [email protected]
users[5] = 
users[6] = 63515987 
users[7] = Joe 
users[6] = Schmoe 
users[8] = [email protected]
users[9] = 

I want to loop this array and store the data for each user in a database, however I have no clue how to verify that I am storing the right information in the place because there is no pattern in the array. Since Joe doesn't have a phone number, his email is would be stored as his phone number. This would result in ever subsequent entry to be off by 1 index.

Any ideas on how to go about this?

P.S. I am using node.js

EDIT: here is an example of the actual data

021870143-14        
lastName    
firstName           
U   
5/16/1988   
11/6/2008   
A   
11/6/2008 6:0:2 
NF  
245         
MAIN ST.    
101     
NEW YORK    
NY
10002                           
11/4/2008   
34      
SD1 
MUNC1J-036  
MAG1-1  
LEG77   
SENT34  
CONG5   
CNTY                
34-1                
10/27/2008 19:59:53                             
NF  
8
  • 2
    I would use regular expressions to tell emails and telephone numbers apart. Commented Oct 8, 2013 at 5:03
  • That would work on this example to some degree, but the actual data that I'm working with has ~30 values for each user. Many of which are strings that don't have any pattern either. Commented Oct 8, 2013 at 5:08
  • 1
    I.E. I don't think there is a way to differentiate "NEW YORK" from "BOB SMITH" with an expression. If there is I'm not sure I'd know how to do it. Commented Oct 8, 2013 at 5:10
  • 1
    Post some actual data then, please. Commented Oct 8, 2013 at 5:18
  • I posted a sample user. I removed all personal information for generic info. Commented Oct 8, 2013 at 5:37

2 Answers 2

1

Here's pseudo code because I don't know javascript. I'm basing this off the fact that I think Javascript has dictionaries/associative arrays/hash tables etc.

new_user = {}
for i from 0 to arr.length
    if arr[i] = null/Omitted field/nil/None/whatever
        database.add_entry(new_user) // Add finished user table to database
        new_user = {} // Start new dictionary
    else
        field = arr[i]
        if field contains '@'
            new_user.email = field
        else if field contains '-'
            new_user.phone_number = field
        else if is_a_number(field)
            new_user.id = field
        else if new_user has first_name
            new_user.last_name = field
        else
            new_user.first_name = field
Sign up to request clarification or add additional context in comments.

7 Comments

I guess I just need to figure out some way to make each field identifiable in some way.
Yes of course. That's what my pseudo code basically does. But it's pseudo code so you have to figure out how to implement it in reality. I hope I helped break down the problem for you though. First check if it's an email using '@' character because a phone number or a name or an id won't have '@'. Then check for dashes like '-' because names most likely won't have dashes (and if they do then you can check for dashes and numbers together). Then if its just a number, it must be an id. After that, if your table has a first name, then it must be a last name, and otherwise you have a first name.
Thank you for your help, but I'm not sure if we are on the same page. Those 5 fields are not the real data. I updated my post to represent the data I am actually using. As you can see, there are many more fields. Most notably how do I tell the difference between City Name and First Name, or the various dates. These items have no distinguishable characteristics.
@Dan Dates have slashes right? There must be some kind of format that each data field follows. As for the city names, are they capitalized or are they like 'Munich' for example? Maybe you better start looking into regular expressions. But if there is really no distinction between the format for city Names and first names then it is hopeless because some cities are named after people and you will never be able to tell if Dallas is a city in Texas or a first name. :)
Haha indeed they are. Also, everything is in all caps...I'll keep at it. Thanks for the help!
|
0

Use tokenization ..

example:

users[0] = "45049345,Bob,Smith,789-456-1230,[email protected]";
users[1] = "63515987,Joe,Schmoe,-,[email protected]";

Process:

for (user in users)
{
    for (detail in user)
    {
        document.write (detail + "<br>");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.