Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic variable name 2

Status
Not open for further replies.

danusko

Technical User
May 25, 2015
10
DE
Hi ,
i try to figured out, how to set in VBSCRIPT some data to variables. I know, how to use basic variables and how to set them, but i need something other.
My situation:
I have table and there are some data in rows. We can tell, that there are CITYs.
I have variable Data and i want to fill this CITYs to my variable data.
Something like
Data1 = NewYork
Data2 = London
Data3 = Praha
...
And i want only define Data(n) and using Do or For i want to set it. But i was not succsesfull. Pls. can you help me? I googled a lot, but it doesnt worked for me. I found something about array and also about collection but e.g. this doesnt worked because it return error "Company1 is undefined"
Code:
Sub Macro_Master()

' create an array with a fixed size
dim companies(2) 

' fill the array with the companies
set companies(0) = Company1
set companies(1) = Company2
set companies(2) = Company3

' iteration example 1
dim company
for each company in companies
    response.write company.CompanyName
next

' iteration example 2
dim i
for i = 0 to ubound(companies)
    response.write companies(i).CompanyName
next
 end sub

thx for help
 
i dont know, because this is only code from internet that i found and wanted to to run. I tried also get company as string like "company" but of course - set is used with object. So it also didnt worked.

For me is important how can i set something to variable with dynamic name.
If i should be concrete, i use CORE04 client and i want to variable set some data from the screen.

part of my code:
Code:
f = autECLSession.autECLPS.GetText(6,4,80)
			g = autECLSession.autECLPS.GetText(7,1,80)
			h = autECLSession.autECLPS.GetText(8,1,80)
			i = autECLSession.autECLPS.GetText(9,1,80)
			j = autECLSession.autECLPS.GetText(10,1,80)
			k = autECLSession.autECLPS.GetText(11,1,80)
			l = autECLSession.autECLPS.GetText(12,1,80)
			m = autECLSession.autECLPS.GetText(13,1,80)
			n = autECLSession.autECLPS.GetText(14,1,80)
			o = autECLSession.autECLPS.GetText(15,1,80)
			p = autECLSession.autECLPS.GetText(16,1,80)
			r = autECLSession.autECLPS.GetText(17,1,80)
			s = autECLSession.autECLPS.GetText(18,1,80)
			t = autECLSession.autECLPS.GetText(19,1,80)
			u = autECLSession.autECLPS.GetText(20,1,80)
			x = autECLSession.autECLPS.GetText(21,1,80)

			data1 = f + vbCrLf + g + vbCrLf + h + vbCrLf + i + vbCrLf + j + vbCrLf + k + vbCrLf + l + vbCrLf + m _
			+ vbCrLf + n + vbCrLf + o + vbCrLf + p + vbCrLf + r + vbCrLf + s + vbCrLf + t + vbCrLf + u + vbCrLf + v + vbCrLf + x + vbCrLf

But i want to use something like loop, because i dont know, how many screen i will get to Data variable. So i wanted use something like
Code:
for i = 1 to 10
data(i) = f + vbCrLf + g + vbCrLf + h + vbCrLf + i + vbCrLf + j + vbCrLf + k + vbCrLf + l + vbCrLf 
msgbox data(i)
next  i

i know, maybe i am not very clear, because my bad english :(
sorry for this.
 
this works in Vbscript
Code:
' create an array with a fixed size
dim companies(2) 

' fill the array with the companies
companies(0) = "Company1"
companies(1) = "Company2"
companies(2) = "Company3"

' iteration example 1
dim company
for each company in companies
    wscript.echo company
next

wscript.echo

' iteration example 2
dim i
for i = 0 to ubound(companies)
    wscript.echo companies(i)
next

Output
Code:
C:\>cscript /NoLogo danusko.vbs
Company1
Company2
Company3

Company1
Company2
Company3
 
ok, but is it possible to "fill array" with loop?
i mean, can i fill it as i wrote?

something like
Code:
for i = 1 to 10
data(i) = f + vbCrLf + g + vbCrLf + h + vbCrLf + i + vbCrLf + j + vbCrLf + k + vbCrLf + l + vbCrLf 
msgbox data(i)
next  i

because i dont know if it will be 3 lines of data or 15. So i want to use DO or FOR for it.
I this possible?

with other words:
i will put in my variable data(i) other variables like city1, city2, city3...
Name of variables city1, city2, city3... will be alwasy the same. There are fixed. But name of variable DATA will change base cyclus. Like:
for i = 1 to 5
msgbox Data(i)
next
this show me content of variable Data1, then Data2....
or this is not possible?

I dont know, i you know what i mean.
 
StongM's question really needs to answered. Where are the values you plan to set coming from?

You can dynamically populate an array. With each new value you would use a redim preserve to extend out the length of the array and then simply set the value of the index to your new value. Where however do you read that value from to set it?

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
The values coming from my client CORE04 from screen, where I set rows which should be set to my variable Data(n). E. g. From actual screen set rows 3 to 10 to my Data1,next screen will go to Data2... then Data3...
 
From my understanding, based on this code that you posted ...

Code:
f = autECLSession.autECLPS.GetText[highlight lightblue](6,[b]4[/b],80)[/highlight]
g = autECLSession.autECLPS.GetText(7,1,80)
h = autECLSession.autECLPS.GetText(8,1,80)
i = autECLSession.autECLPS.GetText(9,1,80)
j = autECLSession.autECLPS.GetText(10,1,80)
k = autECLSession.autECLPS.GetText(11,1,80)
l = autECLSession.autECLPS.GetText(12,1,80)
m = autECLSession.autECLPS.GetText(13,1,80)
n = autECLSession.autECLPS.GetText(14,1,80)
o = autECLSession.autECLPS.GetText(15,1,80)
p = autECLSession.autECLPS.GetText(16,1,80)
r = autECLSession.autECLPS.GetText(17,1,80)
s = autECLSession.autECLPS.GetText(18,1,80)
t = autECLSession.autECLPS.GetText(19,1,80)
u = autECLSession.autECLPS.GetText(20,1,80)
x = autECLSession.autECLPS.GetText(21,1,80)

data1 = f + vbCrLf + g + vbCrLf + h + vbCrLf + i + vbCrLf + j + vbCrLf + k + vbCrLf + l + vbCrLf + m _
+ vbCrLf + n + vbCrLf + o + vbCrLf + p + vbCrLf + r + vbCrLf + s + vbCrLf + t + vbCrLf + u + vbCrLf + v + vbCrLf + x + vbCrLf

... the above code could be simplified to this:
Code:
For x = 6 to 21
   data(1) = data(1) & autECLSession.autECLPS.GetText(x, 1, 80) & vbCrLf
Next

... well, not quite, because the text in your code above that I highlighted in blue has a different "column" value (4) from the rest (the three parameters in GetText seem to be row, column, length). But this is the idea. You could nest that in a loop:

Code:
For [highlight #FCE94F]i[/highlight] = 1 to 10
   For x = 6 to 21
      data[highlight #FCE94F](i)[/highlight] = data[highlight yellow](i)[/highlight] & autECLSession.autECLPS.GetText(x, 1, 80) & vbCrLf
   Next
Next

But that would make each part of the Data array the same... Which you don't want.

because i dont know if it will be 3 lines of data or 15. So i want to use DO or FOR for it.
This is the problem, which is what strongm and markdmac are asking... You need to figure out what values you want in the Data array and how you can get them there.
 
danusko said:
...E. g. From actual screen set rows 3 to 10 to my Data1,next screen will go to Data2... then Data3...
If I understand right, you mean that every screen could have different number of lines you need to store?
In this case I would use a flexible data structures called associative array (aka hash, aka dictionary), i.e. I would create a hash of screens and every element of it would be a hash of lines.

Here is an example, how would I create the data structure and iterate over it:
hash2D.vbs
Code:
[COLOR=#0000ff]' create screens Dictionary object[/color]
[COLOR=#804040][b]set[/b][/color] screens [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.Dictionary"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#0000ff]' store lines of screen 1[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]createObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.Dictionary"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line1"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line01 of Screen01"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line3"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line03 of Screen01"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line5"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line05 of Screen01"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line9"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line09 of Screen01"[/color]
[COLOR=#0000ff]' add lines to screen[/color]
screens[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"screen1"[/color][COLOR=#804040][b],[/b][/color]  [COLOR=#a020f0]lines[/color]
[COLOR=#0000ff]' dispose line object[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

[COLOR=#0000ff]' store lines of screen 2[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]createObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.Dictionary"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line2"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line02 of Screen02"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line4"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line04 of Screen02"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line6"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line06 of Screen02"[/color]
[COLOR=#0000ff]' add lines to screen[/color]
screens[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"screen2"[/color][COLOR=#804040][b],[/b][/color]  [COLOR=#a020f0]lines[/color]
[COLOR=#0000ff]' dispose lines object[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

[COLOR=#0000ff]' store lines of screen 3[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]createObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.Dictionary"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line10"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line10 of Screen03"[/color]
[COLOR=#a020f0]lines[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"line20"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"Line20 of Screen03"[/color]
[COLOR=#0000ff]' add lines to screen[/color]
screens[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] [COLOR=#ff00ff]"screen3"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#a020f0]lines[/color]
[COLOR=#0000ff]' dispose lines object[/color]
[COLOR=#804040][b]set[/b][/color] [COLOR=#a020f0]lines[/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]


[COLOR=#0000ff]' to see what's in hash:[/color]
wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"Screens and associated lines"[/color]
[COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] scr [COLOR=#804040][b]in[/b][/color] screens[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]keys[/color]
  wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"-----------------------"[/color]
  wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"This is screen: "[/color] [COLOR=#804040][b]&[/b][/color] scr
  wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"-----------------------"[/color]
  [COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] ln [COLOR=#804040][b]in[/b][/color] screens[COLOR=#804040][b]([/b][/color]scr[COLOR=#804040][b])[/b][/color]
    wscript[COLOR=#804040][b].[/b][/color]echo screens[COLOR=#804040][b]([/b][/color]scr[COLOR=#804040][b]).[/b][/color][COLOR=#a020f0]item[/color][COLOR=#804040][b]([/b][/color]ln[COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]next[/b][/color]
[COLOR=#804040][b]next[/b][/color]

[COLOR=#0000ff]' dispose screens object[/color]
[COLOR=#804040][b]set[/b][/color] screens [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

Output:
Code:
C:\Work>cscript /NoLogo hash2D.vbs
Screens and associated lines
-----------------------
This is screen: screen1
-----------------------
Line01 of Screen01
Line03 of Screen01
Line05 of Screen01
Line09 of Screen01
-----------------------
This is screen: screen2
-----------------------
Line02 of Screen02
Line04 of Screen02
Line06 of Screen02
-----------------------
This is screen: screen3
-----------------------
Line10 of Screen03
Line20 of Screen03



 
Thx for your help.
And no, the number of lines are always the same.
The goal of my question is.
What is the easiest way to make variable for storing data and then resoring them.
This is the reason, why i look for something like dynamic name of variable e.g. Data(n).
Because i dont know, how many screen i will store. So i want, that in some cycle or loop it will store automaticaly to variable Data(n), where "n" will by the number of cycle.

So it will go to screen one and store e.g. text for cycle 1 to Data 1, then will continue to cycle 2 to Data2....
I know, that my explanation make it harder for your help. :(
I belive, that when i tell, e.g. that my maximum number of screen is 15, it will not neccessary to make 15 times "add" function in code for store every 15 screens. And this is also reason, why i search something like loop and dynamic name of variable.

 
If Guitarzan's code solves your issue please remember to click [Great Post?Star it!]

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
sorry I miss your code, I will try it. . But now I am not on my comp. I will try it after 15 hours. Then will here 9 a. m.
Thx a lot
 
Hi to all,
i trie code of guitarzan and of course it worked exactly as i needed.
Thank you very much for help.
 
how can i generate the system name, used id logged in and system OS in to a csv file by vb script?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top