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 3328
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 +...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |