473,765 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to create an Array with parametrized name?

hello everybody,

is there a way of creating an array with help of a function that would
accept the name of this array as a parameter and then create global
Array type variable of that name?
so that for example the following code would work as well in browsers as
under Windows Scripting Host:

str = "tableA";

fDoArray(str);
fDoArray("table B");

tableA[10] = 10;
tableB[11] = 11;

I would appreciate any suggestions and help,
regards,
mirek
Jul 20 '05 #1
22 2760
Hello,

<script>
function fDoArray(arrayN ame)
{
window[arrayName] = new Array();
}

str = "tableA";

fDoArray(str);
fDoArray("table B");

tableA[10] = 10;
tableB[11] = 11;

alert(tableB);
alert(tableA);
</script>

--
Elias
http://lgwm.org/

"nobody" <no****@non.exi ste.nt> wrote in message
news:b2******** *************@n ews.chello.at.. .
hello everybody,

is there a way of creating an array with help of a function that would
accept the name of this array as a parameter and then create global
Array type variable of that name?
so that for example the following code would work as well in browsers as
under Windows Scripting Host:

str = "tableA";

fDoArray(str);
fDoArray("table B");

tableA[10] = 10;
tableB[11] = 11;

I would appreciate any suggestions and help,
regards,
mirek

Jul 20 '05 #2
"nobody" <no****@non.exi ste.nt> wrote:
is there a way of creating an array with help of a function that would
accept the name of this array as a parameter and then create global
Array type variable of that name?
so that for example the following code would work as well in browsers as
under Windows Scripting Host:

str = "tableA";

fDoArray(str);
fDoArray("table B");

tableA[10] = 10;
tableB[11] = 11;


function fDoArray(str){
window[str]=new Array();
return window[str];
}
Jul 20 '05 #3
hello,

thanks Vjekoslav and Elias. do you know any solution, that would also
work under Windows Scripting Host?

cheers,
mirek
Jul 20 '05 #4


nobody wrote:
is there a way of creating an array with help of a function that would
accept the name of this array as a parameter and then create global
Array type variable of that name?
so that for example the following code would work as well in browsers as
under Windows Scripting Host:

str = "tableA";

fDoArray(str);
fDoArray("table B");

tableA[10] = 10;
tableB[11] = 11;


I think using this works as follows:
function output (text) {
if (typeof document != 'undefined' && typeof document.write !=
'undefined') {
document.write( text + '<br>\n');
}
else if (typeof WScript != 'undefined') {
WScript.Echo(te xt);
}
}

function createArray (variableName) {
this[variableName] = [];
}

createArray('ar rayName');
arrayName[0] = 'Kibo';
arrayName[1] = 'Maho';

output(arrayNam e);
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #5
> thanks Vjekoslav and Elias. do you know any solution, that would also
work under Windows Scripting Host?


The global object does not have a standard name, which is sort of surprising. In
browsers, it can be referred to as 'window'. In other environments it might have
no name at all.

The 'this' variable, when used outside of any function, refers to the global
object. So, at the top of your program, write

var global = this;

and then use 'global' in place of 'window'.

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

Jul 20 '05 #6
Martin and Douglas -- thank you both for help and WSH solution

cheers :)
mirek
Jul 20 '05 #7
Hi,

"nobody" <no****@non.exi ste.nt> wrote in message
news:XB******** *************@n ews.chello.at.. .
| hello,
|
| thanks Vjekoslav and Elias. do you know any solution, that would also
| work under Windows Scripting Host?

original message:

is there a way of creating an array with help of a function that would
accept the name of this array as a parameter and then create global
Array type variable of that name?
so that for example the following code would work as well in browsers as
under Windows Scripting Host:
---

While you're looking for a JS solution, you may want to consider ...

This is easy in VBS - not sure about JS. Although undocumented, the VBS
Execute statement will create a new global variable (despite any Option
Explicit statement), if it does not find the variable name referenced in
either its immediate local variable list or the global variable list. So
the following should work (air code):

sub NewArray (rsArrayName)
execute "dim " & rsArrayName & "()"
end sub

Just Redim after the call, as needed, or you could incorporate the
dimensions and bounds through an additional string parameter, if you wish.
You can wrap the code in On Error ... statements, if you're worried about
potentially Dimming an existing array. You can mix JS and VBS in HTML or
WSC wrappers.

Joe Earnest

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 09-23-03
Jul 20 '05 #8
Uzytkownik "Douglas Crockford" <no****@covad.n et> napisal w wiadomosci
news:73******** *************** ****@msgid.mega newsservers.com ...
....
The 'this' variable, when used outside of any function, refers to the global object. So, at the top of your program, write

var global = this;


using try...catch may cause the same code work both in browser and wsh.

var wsh
try{window=this ;wsh=1}catch(e) {}

function fDoArray(x){ret urn window[x]=[]}

fDoArray("table A")
tableA[0]=0
tableA[1]=1
tableA[2]=2

if(wsh){
Shell=WScript.C reateObject("WS cript.Shell")
Shell.Popup(tab leA)
}
else alert(tableA)

greets
BlaTek
http://blatek.25.pl/christmaspuzzle/puzzle.html
Jul 20 '05 #9
Bogdan Blaszczak wrote:
using try...catch may cause the same code work both in browser and wsh.

var wsh
try{window=this ;wsh=1}catch(e) {}


This may fail to give the right results if running under some other host
such as ASP. I'm trying to imagine the purpose of the original posters
request?

--
Gerry Hickman (London UK)

Jul 20 '05 #10

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

Similar topics

7
8869
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
3
2225
by: ESPNSTI | last post by:
Hi, I'm looking for a way to associate multiple "lists" of constants to each other. For example, I'd like a way to look up a database column's name based on its "number" in a constants list. I'm new to C# and my background is in Delphi, so below is an example of how I would declare such a constant array of records in Delphi. ______________________________________________________
23
7416
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
0
1024
by: Halimaji Nijazi | last post by:
Hi everybody I've read a lot about parametrized services and now I want to test it. My problem is, how should I create a simple parametrized service? How starting this service whithin .NET? Do someone has samples or sample code or any step-by-step instructions? Thanks alot
10
3992
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item in the list A i want to associate an undetermined number of items. The complication for me is the fact that the items to associate in list A are undetermined.
7
3782
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless, here it is: I wish to create an two-dimensional array of objects. On the one hand, it is useful to use STL containers for the rows and columns of the array. On the other, it would be nice to address the elements by the array element operator ...
4
2588
by: usunto_bryjamus | last post by:
Hi, Is it possible to parametrize Property grid category? I have class: public class Test { private string name;
3
4486
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
8
1739
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.