I have a list of input strings and I want to take each string and using the split method populate a string array, do some stuff with it and that process is in a "for" loop. After each pass through the "for" loop I want to re-use the same string array. And, as all you fundi's in c# will know, the array just keeps getting added to. How do I say start from scratch each time? I know this is going to be real easy, and I'm going to feel real foolish once it is pointed out to me, but please point it out to me!!!!
You mean...you want to split all the strings and make it a string array? - private void button1_Click(object sender, EventArgs e)
-
{
-
string line = "";
-
string pathNames = "D:\\textfile.txt";
-
ArrayList rawData = new ArrayList();
-
try
-
{
-
StreamReader readFile = new StreamReader(pathNames);
-
while (line != null)
-
{
-
string[] sa = line.Split(' ');
-
foreach (string s in sa)
-
{
-
if (s != "")
-
rawData.Add(s);
-
}
-
line = readFile.ReadLine();
-
}
-
}
-
catch (Exception f)
-
{
-
MessageBox.Show(f.Message);
-
}
-
string da = "";
-
int rIn = rawData.Count;
-
string[] data = new string[rIn];
-
for (int i = 0; i < rIn; i++)
-
{
-
data[i] = rawData[i] + "";
-
da += "data[" + i + "] = " + rawData[i] +"\n";
-
}
-
MessageBox.Show(da);
-
}
11 4458
Thanks Adriancs, but doesn't answer my question. It was using dotnetperls that gave me the idea to use the split method. Say that I read, using StreamReader, strings of data into memory:
a1 a2 a3
b1 b2 b3
c1 c2 c3 etc. -
ArrayList rawData = new ArrayList();
-
try
-
{
-
StreamReader readFile = new StreamReader(pathNames);
-
while (line != null)
-
{
-
line = readFile.ReadLine();
-
rawData.Add(line);
-
}
-
}
-
catch (Exception f)
-
{
-
MessageBox.Show(f.Message);
-
}
-
int rIn = rawData.Count;
-
for (int i = 0; i < rIn; i++)
-
{
-
string[] data = rawData[i].Split();
-
-
// Do a whole bunch of stuff, up to here everything works as expected
-
}
-
-
// I get:
-
// data[0] = "a1"
-
// data[1] = "a2"
-
// data[3} = "a3"
-
// However it's on the next iteration i = 1, I get the following
-
// data[0] = "a1"
-
// data[1] = "a2"
-
// data[3} = "a3"
-
// data[4] = "b1"
-
// data[5] = "b2"
-
// data[6} = "b3"
-
// And I just want
-
// data[0] = "b1"
-
// data[1] = "b2"
-
// data[3} = "b3"
-
-
How do I tell my program to start filling ‘data’ array from '0' again?
Hello Joe ,
I am not sure if this works...
Declare the string array as
string[] data = null;
outside the for loop or try adding
data = null;
just before the closing brace of for loop.....
Hello Shilpa,
Thanks for trying, but no, it doesn't work. To be honest I had already tried the data = null rout inside the for loop, but this time I added the string data[] = null also, but without any success, but thank you for making the suggestion.
Joe
well, perhaps there's some logic error occur somewhere at the unseen code.
I have try this, and it seems to be what you want.
you may need to double check your coding.
I have test the below with a text file.
The content of the text(D:\textfile.txt): -
yellow blue green red
-
tea juice milk coffee
-
car bus lorry van
the code: - public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
string line = "";
-
string pathNames = "D:\\textfile.txt";
-
ArrayList rawData = new ArrayList();
-
try
-
{
-
StreamReader readFile = new StreamReader(pathNames);
-
while (line != null)
-
{
-
line = readFile.ReadLine();
-
rawData.Add(line);
-
}
-
}
-
catch (Exception f)
-
{
-
MessageBox.Show(f.Message);
-
}
-
int rIn = rawData.Count;
-
for (int i = 0; i < rIn; i++)
-
{
-
string[] data = (rawData[i] + "").Split(' ');
-
string da = "";
-
if (data.Length == 1 && data[0] == "") da = "Finish.";
-
else
-
{
-
for (int j = 0; j < data.Length; j++)
-
{
-
da += "data[" + j + "] = " + data[j] + "\n";
-
}
-
}
-
MessageBox.Show(da);
-
}
-
}
-
}
You have you debugged and verified the status of rawData? The code as you posted it throws away the data[] variable every time split is called, just as if you set an int from 3 to 5, the 3 is thrown out.
In order to get what you described the next instance of rawData must have the data from the previous in it: - rawData[0] = "a1,a2,a3"
-
rawData[1] = "a1,a2,a3,b1,b2,b3"
-
rawData[2] = "a1,a2,a3,b1,b2,b3,c1,c2,c3"
I have seen errors like this in reading text files when reusing the same variables when populating a data arrays.
Edit: seems adriancs and I were typing at the same time, but we're both getting at the same issue I think.
You mean...you want to split all the strings and make it a string array? - private void button1_Click(object sender, EventArgs e)
-
{
-
string line = "";
-
string pathNames = "D:\\textfile.txt";
-
ArrayList rawData = new ArrayList();
-
try
-
{
-
StreamReader readFile = new StreamReader(pathNames);
-
while (line != null)
-
{
-
string[] sa = line.Split(' ');
-
foreach (string s in sa)
-
{
-
if (s != "")
-
rawData.Add(s);
-
}
-
line = readFile.ReadLine();
-
}
-
}
-
catch (Exception f)
-
{
-
MessageBox.Show(f.Message);
-
}
-
string da = "";
-
int rIn = rawData.Count;
-
string[] data = new string[rIn];
-
for (int i = 0; i < rIn; i++)
-
{
-
data[i] = rawData[i] + "";
-
da += "data[" + i + "] = " + rawData[i] +"\n";
-
}
-
MessageBox.Show(da);
-
}
duplicate post.
This post can be deleted.
Thanks guys, you have pointed me in the right direction, the code I didn’t show. Adrian, for brevity purposes I was not totally expicit, the actual code works on a split of a split. So, for the first split I also get the same result with the code I did not supply as you get with the code you supplied. It's when I get to the next step that I ran into the problem, and, as Hamlet say’s, therein lies the rub! I have been sticking the result of my first split into a string, and that code was wrapped in a #region/#endregion , and out of sight out of mind, I have not been clearing out the string from the first split. I think I am now going to hang myself :-)
Hi, you have to mark "choose as best answer" button on the most appropriate post that has solved your problems. :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Craig Bailey |
last post by:
Anyone recommend a good script editor for Mac OS X?
Just finished a 4-day PHP class in front of a Windows machine, and liked
the editor we used. Don't recall the name, but it gave line numbers as...
|
by: Chris |
last post by:
Sorry to post so much code all at once but I'm banging my head against
the wall trying to get this to work!
Does anyone have any idea where I'm going wrong?
Thanks in advance and sorry again...
|
by: James |
last post by:
My form and results are on one page.
If I use :
if ($Company) {
$query = "Select Company, Contact
From tblworking
Where ID = $Company
Order By Company ASC";
}
|
by: Beowulf |
last post by:
Hi Guru's,
I have a query regarding using PHP to maintain a user profiles list. I
want to be able to have a form where users can fill in their profile
info (Name, hobbies etc) and attach an...
|
by: David Hickman |
last post by:
Hi all,
This is a newbie question but I have been struggling with this for ages. I
have a document with 2 separate <?php sections of code. I would like to
declare some variables in the first...
|
by: JW |
last post by:
I wanted have this as part of a flood control script:
<?
echo ("Flood control in place - please wait " . $floodinterval . " seconds
between postings.");
sleep(5);
// go back two pages
echo...
|
by: Lukas |
last post by:
I want to send an email directly to a directory on my server
what script do I need to do this?
Maybe I should add a line to my aliases file like this: emailaddress:
"|/usr/bin/email2directory" ?...
|
by: Chris Krasnichuk |
last post by:
Hello every one,
Does anyone know how to make php work on your computer?
please reply I need help
Chris
|
by: Frans Schmidt |
last post by:
I want to make a new database with several tables, so I did the following:
<?php
CREATE DATABASE bedrijf;
CREATE TABLE werknemers
(voornaam varchar(15),
achternaam varchar(20),
leeftijd...
|
by: sky2070 |
last post by:
Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ')'
in c:\inetpub\wwwroot\session.php on line 19
can anyone tell me what is wrong with this code???
<?
// Define the Session...
|
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: 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...
|
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: 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: 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: 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,...
| |