How can I read array values without using key/index value?

R

Randell D.

Folks,

Here's my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

Thanks,
Randell D.
 
D

Douglas Crockford

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

Buy a book, man. You are dealing with several misunderstandings. Some people
enjoy being willfully ignorant. I sense that you are not one of those people.

In JavaScript, a collection of named values is called an object. The values are
unordered. You can use a for..in statement to retrieve the values and ignore the
odd ones, but the order is not guarenteed to be stable.

JavaScript has an array type which takes integer subscripts. This is probably
what you want. If so, where did you get the nutty idea that you had to use
letters as subscripts?

http://www.crockford.com/javascript/javascript.html
 
L

Lee

Randell D. said:
Folks,

Here's my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

The obvious solution seems to be to change your PHP code to create it
the way that you want to be able to access it:

example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";
 
R

Randell D.

Douglas Crockford said:
I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

Buy a book, man. You are dealing with several misunderstandings. Some people
enjoy being willfully ignorant. I sense that you are not one of those people.

In JavaScript, a collection of named values is called an object. The values are
unordered. You can use a for..in statement to retrieve the values and ignore the
odd ones, but the order is not guarenteed to be stable.

JavaScript has an array type which takes integer subscripts. This is probably
what you want. If so, where did you get the nutty idea that you had to use
letters as subscripts?

http://www.crockford.com/javascript/javascript.html

I'm broke - no work and little savings to carry me forward until a work
permit gets approved... Secondly, I have put a javascript book on my xmas
wish list... I'm reliant on old javascript books and online guides that I
read. I guess my nutty idea of using letters as subscripts might have
originated from PHP but, while I can't recall where, I thought I'd seen an
examlpe of someone who had done such (ie used letters (and not variables) to
identify elements in an array.

Anyway... I'll work with the for...in loop and see how that helps me... I
can't have PHP pre-assign the numeric subscript portion of the array as
several javascript files (containing arrays) are created in advance and web
pages include as many, or as few of them as are needed... I want to make
sure there are no duplicates over-write preexisting array elements....

Thanks for the help though...
 
R

Randell D.

Lee said:
Randell D. said:
Folks,

Here's my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

The obvious solution seems to be to change your PHP code to create it
the way that you want to be able to access it:

I can't have PHP pre-assign the numeric subscript portion (what I call
element keys or indexes) of the array as several javascript files
(containing arrays) are created in advance and web pages include as many, or
as few of them as are needed... I want to make sure there are no duplicate
array elements which might over write an array element that might be
included from anothre file.

Thanks though...
 
E

Eric Bohlman

I'm broke - no work and little savings to carry me forward until a
work permit gets approved... Secondly, I have put a javascript book on
my xmas wish list... I'm reliant on old javascript books and online
guides that I read. I guess my nutty idea of using letters as

Go over to <http://devedge.netscape.com/library/manuals/> and download the
"JavaScript 1.5 Core Guide" and "JavaScript 1.5 Core Reference."
 
L

Lee

Randell D. said:
Lee said:
Randell D. said:
Folks,

Here's my problem:

I am createing an array in a file using the reslut of some PHP and MySQL
processing. An example of my array would be

examlpe['a']="example one";
examlpe['b']="example two";
examlpe['c']="example three";
examlpe['d']="example four";
examlpe['e']="example five";
examlpe['f']="example six";

The array key (or index) can have different values (always unique though).

I want to be able to reference/read, for example, the second and fourth
element in the array - but since my array keys/indexes are dynamic, my
standard script will only know the array is called 'example'...

So... using the example above, I would not know that the first element in
the array is element['a'].

My Question: Is there a method that I can use to reference any part of the
array, without knowing the elements key?

If not, is there a method to convert the array to have numeric keys?

The obvious solution seems to be to change your PHP code to create it
the way that you want to be able to access it:

I can't have PHP pre-assign the numeric subscript portion (what I call
element keys or indexes) of the array as several javascript files
(containing arrays) are created in advance and web pages include as many, or
as few of them as are needed... I want to make sure there are no duplicate
array elements which might over write an array element that might be
included from anothre file.

If your PHP code creates them in this form:
example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

Then each one will absolutely have a unique number, representing
the order in which it was loaded, regardless of how many different
files they come from.
 
D

Douglas Crockford

Buy a book, man. You are dealing with several misunderstandings.
I'm broke - no work and little savings to carry me forward until a work
permit gets approved... Secondly, I have put a javascript book on my xmas
wish list... I'm reliant on old javascript books and online guides that I
read. I guess my nutty idea of using letters as subscripts might have
originated from PHP but, while I can't recall where, I thought I'd seen an
examlpe of someone who had done such (ie used letters (and not variables) to
identify elements in an array.

You are breaking my heart. That is about the saddest story I have ever seen in
this newsgroup.
 
T

Thomas 'PointedEars' Lahn

Lee said:
example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

Oh my. Why not

var example =
["example one",
"example two",
"example three",
"example four",
"example five",
"example six"];

?


PointedEars
 
G

Grant Wagner

Thomas said:
Lee said:
example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

Oh my. Why not

var example =
["example one",
"example two",
"example three",
"example four",
"example five",
"example six"];

?

PointedEars

Or, if you add elements in several "blocks" and can't always
create a new Array:

var a = new Array(); // [];
a.push(
"example one",
"example two",
"example three",
"example four",
"example five",
"example six"
);
// more code
a.push(
"example seven",
"example eight"
);

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
G

Grant Wagner

Grant said:
Thomas said:
Lee said:
example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

Oh my. Why not

var example =
["example one",
"example two",
"example three",
"example four",
"example five",
"example six"];

?

PointedEars

Or, if you add elements in several "blocks" and can't always
create a new Array:

var a = new Array(); // [];
a.push(
"example one",
"example two",
"example three",
"example four",
"example five",
"example six"
);
// more code
a.push(
"example seven",
"example eight"
);

Forgot to mention, the Array object doesn't support the push() method in IE on the Mac,
and while it's mostly a dead browser, the problem can be worked around with:

var test = new Array();
if (typeof test.push == "undefined") {
function push() {
for (var i = 0 ; i < arguments.length ; i++) {
this[this.length] = arguments;
}
}
Array.prototype.push = push;
}

I'm sure this could be streamlined, but I don't have a Mac to test it on, and I'm sure
the above works because we've verified it.

if (typeof (new Array()).push == "undefined") {
Array.prototype.push = function() {
for (var i = 0 ; i < arguments.length ; i++) {
this[this.length] = arguments;
}
}
}

would probably work as well, although, as I said, I don't have a way to test
Array.prototype.push = function()...

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
R

Randell D.

Thomas 'PointedEars' Lahn said:
Lee said:
example[0]="example one";
example[1]="example two";
example[2]="example three";
example[3]="example four";
example[4]="example five";
example[5]="example six";

or:

example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

Oh my. Why not

var example =
["example one",
"example two",
"example three",
"example four",
"example five",
"example six"];

?


PointedEars

Thanks... but I think I prefer the
example[example.length]="example one";
example[example.length]="example two";
example[example.length]="example three";
example[example.length]="example four";
example[example.length]="example five";
example[example.length]="example six";

It allows me to include more than one javascript file that contains
additional elements for the array which are appended without having to
re-jig the numeric indexes...
 
T

Thomas 'PointedEars' Lahn

Randell said:

It is called attribution _line_.
Oh my. Why not

var example =
["example one",
"example two",
"example three",
"example four",
"example five",
"example six"];

?
[...]

Thanks... but I think I prefer the
example[example.length]="example one";
[...]

It allows me to include more than one javascript file that contains
additional elements for the array which are appended without having to
re-jig the numeric indexes...

You can use both without using indexes on assignment, provided that the
first comes first.


PointedEars

P.S.
Please trim your quotes: <http://www.netmeister.org/news/learn2quote.html>

And use an e-mail address, not spoiling existing namespaces
(here: share.com) and not pissing off communicative people:
<http://www.interhack.net/pubs/munging-harmful/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top