Forum Moderators: open

Message Too Old, No Replies

assiging variables to arrays

         

sssweb

7:29 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



How would I assign a string variable as one of the values of an array? The variable & array are already defined, I just need the syntax for inserting the variable into the array. I tried the following, but it gives me an error:

var string = "\"string variable\""

var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]

I also tried it this way, without success:

var string = "[\"string variable\"]"

var arr = new Array()
arr[0]=string
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]

Bernard Marx

8:21 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand what you mean.
What does "string variable" mean to you? A variable can hold any data type in Javascript.

var string = "\"string variable\""

The variable, string, now holds the string "string variable". I'm guessing this isn't what you want.

sssweb

8:49 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



I mean a variable that holds text data; I used "string variable" as an example -- I'll change the text to suit my needs. Sorry, thought I had the right terminology.

Each value of my array has a text value, in [brackets] and "quotes" as I indicated. I formatted the variable I want to add the same way, but the script returns "undefined" for arr[0].

(FYI, the reason I'm doing this is that, while the rest of my array values are set, this one is calculated/defined earlier in the script, and can be one of two strings depending on the user's system settings.)

sssweb

9:24 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



Maybe this will make it clearer:

if (screen.width<1024) var string = "lo-res text"
else var string = "hi-res text"

var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]

In the above example, arr[0] should =
["lo-res text"]
or
["hi-res text"]
based on the user's screen res.

Bernard Marx

9:40 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Each value of my array has a text value, in [brackets] and "quotes" as I indicated.

Hmm. There is risk of further confusion here.
Are the brackets for our benefit, or do they actually appear in the code?

(Just in case you don't know..) Square brackets can be used in array initialisers - basically a shorthand form of array creation.

These 3 bits of code all do the same thing..

(1)-----------------------------
[blue]var arr = new Array();
arr[0] = "A";
arr[1] = "B";
arr[2] = "C";[/blue]
(2)-----------------------------
[blue]var arr = new Array("A","B","C");[/blue]
(3)-----------------------------
[blue]var arr = ["A","B","C"];[/blue]
--------------------------------

The code you have posted looks like a 2-dimensional array is being created, using a mixture of syntax...

var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]

See what I mean?


Setting the value of an array element is very straightforward. I suspect the problem is somewhere else. Possibly need to see more.

sssweb

9:52 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



The brackets appear in the code. This is exactly how my array looks:

var arr = new Array()
arr[0]=["array value 0"]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]

I want to replace the first value -- ["array value 0"] -- with var string, in a format that will be read properly by the array.

Bernard Marx

9:57 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then you have a 2-D array and the code you initially posted should do it
(if string is defined).

var string = "hello";
//
var arr = new Array();
arr[0]=[string];
arr[1]=["array value 1"];
arr[2]=["array value 2"];
arr[3]=["array value 3"];

In this case:

alert(arr[0][0]) [green]// --> hello[/green]


To add the value after the array has been created:

arr[0] = [string];

Or, if the sub-array, arr[0] has already been created:

arr[0][0] = string;


Why are you using nested arrays? (if you don't mind me asking)

sssweb

11:16 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



I didn't think I was nesting arrays -- one of us isn't clear about what's going on (probably me!)

Review my third post to see what I'm trying to accomplish; that may clarify things. I'm just trying to substitute a script-calculated array value for a pre-set one.

Regarding the code in your last post, when I test it by itself exactly as given, with the alert message, it works fine. But when I use it in my page, I still get 'undefined' for arr[0].

Two questions:

First, regarding quotes, as I indicated, my array values are defined with quotes -- i.e. arr[0]=["quoted text"] . And when I define var string = "text"; I also use quotes. So I've formatted the string definition this way:

var string = "\"text\"";

Is that correct? (As I said, when I test it with the alert, it correctly reads "text" with the quotes included.)

The second point may be causing the glitch. In my effort to simplify the code for posting here, I omitted the fact that each array value actually has two separate strings, so it really looks like this:

var arr = new Array()
arr[0]=["text 0a", "text 0b"]
arr[1]=["text 1a", "text 1b"]
arr[2]=["text 2a", "text 2b"]
arr[3]=["text 3a", "text 3b"]

Accordingly, I defined my string like this (including the if/else conditional):

if (screen.width<1024) {
var string = "\"lo-res text a\", \"lo-res text b\"";
} else
var string = "\"hi-res text a\", \"hi-res text b\"";

When I test it with your alert, it appears correct to me (for hi-res monitor):

"hi-res text a", "hi-res text b" (quotes included)

Bernard Marx

12:04 am on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't think I was nesting arrays -- one of us isn't clear about what's going on (probably me!)

Yes arr[0] to [3] are all arrays themselves.

var string = "\"text\"";

Is that correct?

Yes. You may find it easier to use single quotes for the actual syntax-meaningful quotes. This saves mucking around with backslashes:

var string = '[blue]"text"[/blue]';

However, I'm still confused about this quoting issue, because, although the inserted string will be quoted, the rest of the strings (as defined in the array) are not quoted. The quotes there are just syntactical.

Accordingly, I defined my string like this (including the if/else conditional):

This is where you are falling off the path, I think.

When I test it with your alert, it appears correct to me

Yes, but arr[0] is a string, whilst arr[1]-[3] are arrays (of strings).

The outer array contains 2-element arrays of strings, yet you are creating a single string to replace the first sub-array with. This might not actually matter, depending on how you are treating the array afterwards. For consistency, you could do this (in shorthand):

var screenStrings = screen.width<1024  
? ['"lo-res text a"','"lo-res text b"']
: ['"hi-res text a"','"hi-res text b"'];

alert(arr[0]) will still give the same output (all other things being equal), but that is because the default toString method for an array will produce all the array members (0th - last) converted to strings, and separated by commas.

sssweb

12:41 am on Mar 12, 2006 (gmt 0)

10+ Year Member



Okay, I got it. You were right about the quotes; didn't need them.

Thanks very much.

P.S. Now that I understand the nested arrays, I'm using them because the script came that way from the site where I got it.