473,388 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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


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.

Jul 20 '05 #1
14 4311
> 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

Jul 20 '05 #2
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";

Jul 20 '05 #3

"Douglas Crockford" <no****@covad.net> wrote in message
news:27**************************@msgid.meganewsse rvers.com...
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...
Jul 20 '05 #4

"Lee" <RE**************@cox.net> wrote in message
news:br*********@drn.newsguy.com...
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 thearray, 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...
Jul 20 '05 #5
"Randell D." <re**********************@and.share.com> wrote in
news:JIpCb.676473$6C4.175820@pd7tw1no:
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."
Jul 20 '05 #6

"Eric Bohlman" <eb******@earthlink.net> wrote in message
news:Xn*******************************@130.133.1.4 ...
"Randell D." <re**********************@and.share.com> wrote in
news:JIpCb.676473$6C4.175820@pd7tw1no:
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."


Thanks...
Jul 20 '05 #7
Lee
Randell D. said:


"Lee" <RE**************@cox.net> wrote in message
news:br*********@drn.newsguy.com...
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 uniquethough). >
>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 ofthe >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.

Jul 20 '05 #8
> > 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.
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.

Jul 20 '05 #9
Lee wrote:
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
Jul 20 '05 #10
Thomas 'PointedEars' Lahn wrote:
Lee wrote:
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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 20 '05 #11
Grant Wagner wrote:
Thomas 'PointedEars' Lahn wrote:
Lee wrote:
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[i];
}
}
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[i];
}
}
}

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

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 20 '05 #12

"Douglas Crockford" <no****@covad.net> wrote in message
news:83**************************@msgid.meganewsse rvers.com...
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.
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.


Smart ass!
Jul 20 '05 #13

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Lee wrote:
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...

Jul 20 '05 #14
Randell D. wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...


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/>
Jul 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
Hashtable has a property IsReadOnly. i can't figure out how this gets set. It's not a property you can set directly and i can't find a constructor that takes an RO parameter. How on earth does it...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
19
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
5
by: Tom Cole | last post by:
Let's say I have the following JSON string returned from a server-side process: { values: } I then create a JSON object from it using eval() (tell me if this is not what should be done). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.