I have a number of variables, e.g. X1, X2, X3, X4, X5.
I want to select only the ones that are non zero(there will only be 2 or 3 at any time), and record them as Y1, Y2 and Y3
I want to know the best way of incrementing the name of the variable, e.g. the variable in a loop is X + (num)
I was trying
Num1=1
Num2=1
Do until num1 =5
If X(num1)<>0 then Y(num2) = X(num1) else goto J1
num2 = num2 +1
J1:
num1 = num1 +1
loop
Is this the best way? and if so how do I define X(num1), I am currently getting the error "sub of function not defined"
3 3331 kadghar 1,295
Recognized Expert Top Contributor
I have a number of variables, e.g. X1, X2, X3, X4, X5.
I want to select only the ones that are non zero(there will only be 2 or 3 at any time), and record them as Y1, Y2 and Y3
I want to know the best way of incrementing the name of the variable, e.g. the variable in a loop is X + (num)
I was trying
Num1=1
Num2=1
Do until num1 =5
If X(num1)<>0 then Y(num2) = X(num1) else goto J1
num2 = num2 +1
J1:
num1 = num1 +1
loop
Is this the best way? and if so how do I define X(num1), I am currently getting the error "sub of function not defined"
yeap, try with an array, you can dim it of any size and redim its size.
for example you can do: - Dim myArray(1 to 5) as integer
-
Sub Numbers()
-
Dim NoZero()
-
dim i as integer
-
dim j as integer
-
-
myArray(1)=0
-
myArray(2)=4
-
myArray(3)=5
-
myArray(4)=8
-
myArray(5)=0
-
j=0
-
for i = 1 to 5
-
if myArray(i) <> 0 then
-
j= j+1
-
end if
-
next
-
' j will be the number of no zeros in you array
-
-
if j <> 0 then
-
redim NoZero(1 to j ) 'here you redim the second array
-
else
-
msgbox("all numbers are zero") ' or send an error message
-
end if
-
-
j=1
-
-
for i = 1 to 5
-
if myArray(i) <> 0 then
-
NoZero(j) = myArray(i)
-
j=j+1
-
end if
-
next
-
'here you put the nozero values to the second array
-
End Sub
It's not a great code, but it'll give you an idea of how arrays, dim and redim works out.
Hope that helps
Thanks will give it a go.
yeap, try with an array, you can dim it of any size and redim its size.
for example you can do: - Dim myArray(1 to 5) as integer
-
Sub Numbers()
-
Dim NoZero()
-
dim i as integer
-
dim j as integer
-
-
myArray(1)=0
-
myArray(2)=4
-
myArray(3)=5
-
myArray(4)=8
-
myArray(5)=0
-
j=0
-
for i = 1 to 5
-
if myArray(i) <> 0 then
-
j= j+1
-
end if
-
next
-
' j will be the number of no zeros in you array
-
-
if j <> 0 then
-
redim NoZero(1 to j ) 'here you redim the second array
-
else
-
msgbox("all numbers are zero") ' or send an error message
-
end if
-
-
j=1
-
-
for i = 1 to 5
-
if myArray(i) <> 0 then
-
NoZero(j) = myArray(i)
-
j=j+1
-
end if
-
next
-
'here you put the nozero values to the second array
-
End Sub
It's not a great code, but it'll give you an idea of how arrays, dim and redim works out.
Hope that helps
It is actually possible to reference individual variables in more or less the way you suggested, using a function which accepts the variable name as a string. But it's a lot of work, for no benefit as far as I know. Much better to handle it with an array, as kadghar suggested.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mothra |
last post by:
Here's what I'm trying to do (kill off old Unix logins):
---------------------
$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line eq "old");
push @{$oldsessions}, @line;
$i++;
|
by: Ken |
last post by:
The fact that you can not reassign a variable in XSL is an endless
source of frustration, causing you to jump through all sorts of
non-intuitive hoops.
In this case, however, the lack of reassignment has me totally
perplexed on how to achieve a solution to the following problem:
(These are illustrative fragments)
XML:
|
by: John Wilkinson |
last post by:
Hi,
I am new to XSLT. My problem is that I wish to create an HTML table, and
give each row an incrementing number from 1.This would increment every
itteration of a for-each loop.
The XSLT fragment I have tried is:
<xsl:variable name="test" select="0"></xsl:variable>
<table border="1">
<tbody>
<tr bgcolor="#9acd32">
|
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in advance.
Erik
|
by: Daz |
last post by:
Hello.
Please could somebody explain to me a method for auto-incrementing a
variable name?
I need to append a number to the variable name as it's created as it
will be automated.
For example:
blah1
blah2
| |
by: pozz |
last post by:
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
|
by: mantrid |
last post by:
Hello
I have the following code, where clicking yh1r is supposed to move h1 10px
down and update the value of yh1 by 20 each time its clicked.
what the code actually does is NOT move h1 and instead of incrementing yh1
from 0 to 20 to 40 etc it doubles it and appends it to the original value
each time eg 0 to 20 to 4020 to 804020 to 160804020...
|
by: subramanian100in |
last post by:
I saw this question from www.brainbench.com
void *ptr;
myStruct myArray;
ptr = myArray;
Which of the following is the correct way to increment the variable
"ptr"?
Choice 1 ptr = ptr + sizeof(ptr);
|
by: jwhitby3 |
last post by:
Hi all, I am trying to develop what amounts to a data entry page for the company I work for, (mostly to make my job easier). I think that I am beginning to grasp php, but I am at a loss now. I understand how to use HTML_Table to add a table to a page, and that portion of my project is coming along nicely. The problem is at this point, that I...
|
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...
|
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...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |