473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incrementing a variable name

2 New Member
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 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:

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
Elementary
2 New Member
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 Recognized Expert Expert
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
2874
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++;
2
26308
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:
2
4869
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">
27
8916
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
5
2346
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
10
4581
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
8
2372
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...
53
4751
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);
7
1915
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...
0
7401
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...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
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...
1
5329
isladogs
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...
0
4945
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...
0
3450
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...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
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...

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.