Hello

I know the title is horribly explained....

I want to store about Bob his name (string), last name (string), and age (integer).

Basically that; Im thinking about a 3 demensional array. Would this be correct? Also I want easy; I dont care if it takes up more memory of if it is slower. By that, I mean that I am not going to make a object just for this.

What is the best way to do this?

Recommended Answers

All 17 Replies

Where are you storing the information and what are you planning on doing with it? How you store it will change depending on what your needs are in terms of how the data is used.

Basically that; Im thinking about a 3 demensional array. Would this be correct?

Unless you mean something different by "dimension" than general computer science, I'd say a 3D array is probably a very poor choice, regardless of your answer to the questions above.

My information is presented as a string in the following format:

"Name: Bob Last: Han Age: 36#"

I want to be able to access each of those traits of the string.

Why are you willing to consider creating a 3-dimensional array, but do not want to create an Object? I think using an Object would be the better choice. The coding work on your part would be about the same but I think using an Object would be more convenient. It is easy to use, can be passed quickly and easily into sub-routines, and changes made to the Object within sub-routines would be known outside the sub-routine. So you wouldn't have to manage several variables and pass by reference.

For example:

var personData = new Object();   
 . . .
personData.name = "Bob";
personData.lastname = "Smith";
personData.age = 34;

Using the individual data components is pretty easy with the dot operator.

And if you have to pass the Object into a sub-routine, you can treat it like a normal variable. Inside the sub-routine, it can have a different name, but you just have to remember to access the individual data components by using the same field names that you initially defined them with.

// Pass Object variable into a sub-routine
subroutine_Name (personData, variable1, variable2, etc.);

 . . .

//Within the sub-routine, the Object variable can have a different name

function subroutine_Name(inputObject1, parameter1, parameter2, etc){
 . . .

inputObject1.age = inputObject1.age + 1;

etc.
}

After exiting the sub-routine, the age component of the variable will have been changed and you can use it if you want.

Like I said, I have no intrest in using a object. Does not give me anything that I would need in the future.

I have a string (this string isnt modifiable or anything) in that format and I would need it to be a in a seperate array:

Something like:

array[0]=Name: Bob Last: Jones Age: 36
array[1]=Name: James Last: Brown Age: 29
array[2]=Name: Lloyd Last: Mals Age: 45

And convert it to:

name[0]=Bob
last[0]=Jones
age[0]=36

name[1]=James
last[1]=Brown
age[1]=29

name[2]=Lloyd
last[2]=Mals
age[2]=45

That would fufill the requirements I need.

I think its simple 2D array

$arr[0]['name']=Bob;
$arr[0]['last']=Jones;
$arr[0]['age']=36;
$arr[1]['name']=James;
$arr[1]['last']=Brown;
$arr[1]['age']=29;
$arr[2]['name']=Lloyd;
$arr[2]['last']=Mals;
$arr[2]['age']=45;

echo $arr[1]['name'];

By a three dimensional array, it seems like you mean a two dimensional array where the second dimension is an array of three items:

var peeps = [
    ["Bob","Jones", 36], 
    ["James","Brown", 29], 
    ["Lloyd", "Mals", 45]
];

If nothing else this is better than parallel arrays because related entities are kept together instead of in separate objects.

No, storing them isnt the problem. The problem is correctly storing them when they are presented as:

Name: Bob Last: Han Age: 36#Name: George Last: Alan Age: 45#Name: Amy Last: James Age: 36#

Sorry if that wasnt clear. I need to get that string and pass it to a array. How do I do that?

Where Im at so far:

var arrayofnames= new Array();
var arrayoflast= new Array();
var arrayofages= new Array();

/*PEOPLE IS A STRING WITH THE INFORMATION THAT I HAVE MENTIONED*/

for (l=0;l<people.length;l++)
            {
                if(people.indexOf("Name: ",l)!=-1)
                {
                    //???
                }
            }

Hi riahc3,

you can do the following to parse your string result, hopefully this will give you an idea on how to implement one.

var dataStr = "Name: Bob Last: Han Ken Age: 36#Name: George Last: Alan Age: 45#Name: Amy Last: James Age: 36#",
    storage;

// Split the string using '#' then remove the last empty element in the result
//  Afterwards, go through each results
storage = (dataStr.split("#")).slice(0,-1).map(
                // Check any name & value pattern then run through 
                //  each name and value string
                function(str){
                        return (str.match(/\w+:([\w ]+)(?![\w+:])/g)).map(
                            // Convert the name and value match to array
                            function(str){
                                    return str.match(/[\w]+(?=:)|[\w ]+/g);
                                })  ;
                    });

//Sample Result
[
[["Name"," Bob"],["Last"," Han Keno"],["Age"," 36"]]
...
]
// You just have to trim each values
// to access the value of name bob you have to do this storage[0][0][1], 
//  it will return " Bob"
commented: Not too understandable but its the one Ill use for now +4

Sadly, I perfer to understand what I am doing as that code you put will problably work but the problem is that I do not understand it and wish to apply for simple ideas and methods (like standard arrays). I do not understand slice/map concepts and I HATE using anon functions (one of the worst features in JS, IMO)

I think the code I put I can apply it and use it as well to do what I want. Im just not sure what else to put as I have to get (for example) the name until I find a space. Then from where go to the next "Last: ". So I need some help on that :)

Thank you

Anyone have any ideas?

Member Avatar for stbuchok

I think DavidB has given the best answer. If you don't want to use an object, then feel free to re-invent the wheel. You have said you want to put three different values into one variable, that is basically an object.

var obj = [{
    FirstName: 'Bobby',
    LastName: 'Fischer',
    Age: 12
},{
    FirstName: 'Greg',
    LastName: 'Raimer',
    Age: 42
}];

obj[1].LastName would be Raimer and obj[0].Age would be 12. If the string you are talking about getting comes from a webservice call or something like that, have it return JSON so that it is easy to manipulate in JavaScript.

Bottom line is that I think you should create an object. Anything else will be a hack.

Like I said, no objects. I guess Ill have to use gon1387 method.

Current code:

var dataStr = $j("#people").val(),
            storage;
            // Split the string using '#' then remove the last empty element in the result
            // Afterwards, go through each results
            storage = (dataStr.split("#")).slice(0,-1).map(
            // Check any name & value pattern then run through
            // each name and value string
                function(str){
                return (str.match(/\w+:([\w ]+)(?![\w+:])/g)).map(
                    // Convert the name and value match to array
                    function(str){
                    return str.match(/[\w]+(?=:)|[\w ]+/g);
                                }) ;
                });

var arrdex = new Array();
var arrdey = new Array();
var arrdet = new Array();
var posx=0;
var posy=0;
var post=0;

for (a=0;a<storage.length;a++)
            {
                for (b=0;b<storage[a].length;b++)
                {
                    for (c=0;c<storage[a][b].length;c++)
                    {
                        if ((b==0) && (c==1)) //name
                        {
                            arrdex[posx]=storage[a][b][c];
                            posx=posx+1;
                        }
                        if ((b==1) && (c==1)) //last name
                        {
                            arrdey[posy]=storage[a][b][c];
                            posy=posy+1;
                        }
                        if ((b==2) && (c==1)) //age
                        {
                            arrdet[post]=storage[a][b][c];
                            post=post+1;
                        }


                    }
                }
            }

Works for me.

Member Avatar for diafol

So you've got your data in this form?

Name: Bob Last: Han Age: 36#Name: George Last: Alan Age: 45#Name: Amy Last: James Age: 36#

As a single string or three separate strings? And you want to store the whole string, three separate strings or chunk everything up into a 2D array? I'm a bit lost to be honest.

If you could show what your input data looks like (e.g. above) and how you'd like it as output (array, object etc).

OK, here is the same code but without the combobox:

var dataStr = "Name: Bob Last: Han Age: 36#Name: George Last: Alan Age: 45#Name: Amy Last: James Age: 36#",
            storage;
            // Split the string using '#' then remove the last empty element in the result
            // Afterwards, go through each results
            storage = (dataStr.split("#")).slice(0,-1).map(
            // Check any name & value pattern then run through
            // each name and value string
                function(str){
                return (str.match(/\w+:([\w ]+)(?![\w+:])/g)).map(
                    // Convert the name and value match to array
                    function(str){
                    return str.match(/[\w]+(?=:)|[\w ]+/g);
                                }) ;
                });

var arrdex = new Array();
var arrdey = new Array();
var arrdet = new Array();
var posx=0;
var posy=0;
var post=0;

for (a=0;a<storage.length;a++)
            {
                for (b=0;b<storage[a].length;b++)
                {
                    for (c=0;c<storage[a][b].length;c++)
                    {
                        if ((b==0) && (c==1)) //name
                        {
                            arrdex[posx]=storage[a][b][c];
                            posx=posx+1;
                        }
                        if ((b==1) && (c==1)) //last name
                        {
                            arrdey[posy]=storage[a][b][c];
                            posy=posy+1;
                        }
                        if ((b==2) && (c==1)) //age
                        {
                            arrdet[post]=storage[a][b][c];
                            post=post+1;
                        }


                    }
                }
            }

I hope that clears it up :)

Member Avatar for diafol

Ok, sorry but I can't add anything to what's already been offered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.