473,408 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Incrementing a variable name

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"
Jul 6 '07 #1
3 3328
kadghar
1,295 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. Dim myArray(1 to 5) as integer
  2. Sub Numbers()
  3. Dim NoZero()
  4. dim i as integer
  5. dim j as integer
  6.  
  7. myArray(1)=0
  8. myArray(2)=4
  9. myArray(3)=5
  10. myArray(4)=8
  11. myArray(5)=0
  12. j=0
  13. for i = 1 to 5
  14.     if myArray(i) <> 0 then
  15.        j= j+1
  16.     end if
  17. next
  18. ' j will be the number of no zeros in you array
  19.  
  20. if j <> 0 then 
  21.     redim NoZero(1 to j )  'here you redim the second array
  22. else
  23.     msgbox("all numbers are zero")  ' or send an error message
  24. end if
  25.  
  26. j=1
  27.  
  28. for i = 1 to 5
  29.     if myArray(i) <> 0 then
  30.        NoZero(j) = myArray(i)
  31.        j=j+1
  32.     end if
  33. next
  34. 'here you put the nozero values to the second array
  35. 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
Jul 6 '07 #2
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:

Expand|Select|Wrap|Line Numbers
  1. Dim myArray(1 to 5) as integer
  2. Sub Numbers()
  3. Dim NoZero()
  4. dim i as integer
  5. dim j as integer
  6.  
  7. myArray(1)=0
  8. myArray(2)=4
  9. myArray(3)=5
  10. myArray(4)=8
  11. myArray(5)=0
  12. j=0
  13. for i = 1 to 5
  14.     if myArray(i) <> 0 then
  15.        j= j+1
  16.     end if
  17. next
  18. ' j will be the number of no zeros in you array
  19.  
  20. if j <> 0 then 
  21.     redim NoZero(1 to j )  'here you redim the second array
  22. else
  23.     msgbox("all numbers are zero")  ' or send an error message
  24. end if
  25.  
  26. j=1
  27.  
  28. for i = 1 to 5
  29.     if myArray(i) <> 0 then
  30.        NoZero(j) = myArray(i)
  31.        j=j+1
  32.     end if
  33. next
  34. 'here you put the nozero values to the second array
  35. 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
Jul 7 '07 #3
Killer42
8,435 Expert 8TB
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.
Jul 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
2
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...
2
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...
27
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...
5
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...
10
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...
8
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...
53
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 +...
7
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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,...
0
agi2029
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,...
0
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...

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.