473,396 Members | 1,998 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,396 software developers and data experts.

Nested For Loops

BRawn
28
Hi,

I'm not an expert at nested for loops and I need help with the following...

I need to extract numeric values from a string and 'discard' any alphabetical characters in the same string. In this case, there will always only be a sequence of 2 numeric strings.

The purpose of this is to append station using racks, should the station need expanding at a later stage. Here's what I have...

Expand|Select|Wrap|Line Numbers
  1.  
  2. string stationCoordinates = stationObjects.StationXY;
  3.                             char[] digits = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
  4.                             int rowNumber = 0;
  5.                             int columnNumber = 0;
  6.                             StringBuilder numericString = new StringBuilder();
  7.                             for (int i = 0; i < stationCoordinates.Length; i++)
  8.                             {
  9.                                 for (int j = 0; j < stationCoordinates.Length; j++)
  10.                                 {
  11.                                     if (char.IsNumber(stationCoordinates[i]))
  12.                                     {
  13.                                         numericString.Append(stationCoordinates[i]);
  14.                                         rowNumber = i;
  15.                                     }
  16.                                 }
  17.                             }
  18.  
  19.  
What I need help with is setting the 'rowNumber' and 'columnNumber' variables with the values i and j return, respectively. I want i to be 'rowNumber' and j to be 'columnNumer'.

Lets say, for instance, stationCoordinates' value is "Row33Col44", I want the 33 (i) and 44 (j), which value's should be incremented accordingly in my append method. Any ideas?
Oct 12 '10 #1
1 1409
Christian Binder
218 Expert 100+
Ok, I hope I've understood your question: you have lets say stationCoordinates="Row33Col44" and you want to set two variables, i and j, e.g. i=33 and j=44.

I'd loop over all chars in stationCoordinates with foreach(char ch in stationCoordinates).
Now I'd check ch if it is a valid number. When it is, I'd append it to a temporary string containing digits (as string), otherwise I'd omit it.
When a non-digit is following a digit-character, I'd parse my temporary string and assign it to [i]-variable. Then the same, continuing where I assigned it to i and do the same for j.

Expand|Select|Wrap|Line Numbers
  1. string stationCoordinates = "Row33Col44";
  2. string temp = string.Empty;
  3. int i = -1, j = -1;
  4. foreach(char ch in (stationCoordinates + "X")) {
  5. //this +"X" is a hack, that after "44" there's a change to a non-digit character. Doing it better, you can additionally put the part in the following else-section outside the loop ...
  6.   if(char.IsNumber(ch)) {
  7.     temp += ch;
  8.   } else {
  9.     if(!string.IsNullOrEmpty(temp)) {
  10.       //string is containing numbers
  11.       if(i == -1) //i wasn't initialized yet
  12.         i = int.Parse(temp);
  13.       else //i was initialized, assign to j
  14.         j = int.Parse(temp);
  15.       temp = string.Empty; //reset temp string
  16.     }
  17.   }
  18. }
  19.  
Oct 12 '10 #2

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

Similar topics

3
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
10
by: Roshawn | last post by:
Hi, I am experimenting with nested For...Next loops. My code looks like this: Dim i as Byte Dim itm as Byte For i = 0 to 9 For itm = 0 to 9 'code omitted
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
3
by: tzuriel | last post by:
Hello all, I think nested loops will do what I want, but I can't seem to get them right. I have two tables, members and casts. I run a query as follows: $sql_query="SELECT DISTINCT...
3
by: Luna Moon | last post by:
My friend has three nested loops, each has 10000, 1000, 100 iterations, respectively. What should be the most efficient way of layout out the three nested loops? for (i=0; i<10000; i++) for...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.