473,388 Members | 1,970 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,388 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 3327
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
0
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...

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.