Reading from file into an array

M

Michael

Jesse Houwing said:
Hello Michael,
Hello Michael,

Michael wrote:

Why bother answering if you are not prepared to help?
Is it just to prove how smart you are? I asked for a snippet
or a link.
If you want to decide on the form of help you get, then hire a
consultant. I am sure you can get somebody to do it for somewhere
between 50 and 100 dollars per hour.

If you want free help, then you will have to take it as it comes.

Arne

You guys are all up yourselves! The only reason I am doing
this is to help someone else make set covering algorithms work
faster! To see if C# can do it better than VB6.0. I have spent
many hours on the task. Why? Because as well as helping it
will improve my skills.
Good bye and good riddance!
Micheal,

I'm very sorry you feel this way. And I think it is great that you're
trying to help someone. And should you have formulated your question
differently, showed what you had aready created, or asked for the
most efficient way of doing it, and provided us with the original VB
code, then we would all have dug deep for you to find what you were
looking for, but even then, we'd probably provide you with the
building blocks, good advice and a collective review on your work so
far.

It's all in the way you've worded it...
Its OK Jesse, I've solved it. With no help from the self
appointed cyber police patrolling this group. I'm fixing
the code to this in case some other poor refugee from
VB6 searches the archives from this group. They would
not be expecting so complex a task, especially with the
hand grenade of double spaces between some of the .txt
data.
// Snippet for reading integers from .txt into a 2 d array
int[,] theLines = new int[163, 6]; //holds 163*6 elements
string path = @"C:\Documents and
Settings\Michael\Desktop\Wheel.txt";
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate,
FileAccess.Read));
int idx = 0;//
while (idx<163)
//THERE ARE DOUBLE SPACES BETWEEN SOME NUMBERS IN .txt
{
string row = textIn.ReadLine();
row = row.Trim();
row = row.Replace(" "," ");//get rid of double spaces
string[] columns = row.Split(' ');//redeclare in case
size changes

for (int j = 0; j < 6; j++)
theLines[idx,j ] = Convert.ToInt32(columns[j]);
idx++;

}

//Check all is well
for (int i = 0; i < 163; i++)
for (int j = 0; j < 6; j++)
{
Console.WriteLine(theLines[i, j]);
}
textIn.Close();

You can simplify your double space prblem by using the
String::Split Method (String[], StringSplitOptions)

overload. See http://msdn.microsoft.com/en-us/library/tabh47cf.aspx. Like
so:

row.Split(new string[]{" ", " "}, StringSplitOptions.None);

Or you could use the StringSplitOptions.RemoveEmptyEntries like this:

row.Split(' ', StringSplitOptions.RemoveEmptyEntries);

This last option should negate the need to use row.Trim as well.

Another thing to note, is that it would be best to use a using(Disposable
object){} to ensure proper cleanup of your stream objects

I also added some error checking here and there...

public int[,] ParseFile(string path)
{
int[,] result= new int[163, 6]; //holds 163*6 elements

if (!File.Exists(path))
{
throw new FileNotFoundException();
}

using (StreamReader textIn = new StreamReader(new FileStream(path,
FileMode.Open, FileAccess.Read)))
{
while (int idx = 0; idx < 163; idx++)
{
if (textIn.Peak() == -1) // check for end-of-file
{
throw new Exception(string.Format("Unexpected end of file at line
{0}", idx + 1));
}

string row = textIn.ReadLine();
string[] columns = row.Split(' ',
StringSplitOptions.RemoveEmptyEntries); // StringSplitOptions removed
double spaces and trims values.
if (colums.Length == 6) // check expected input
for (int j = 0; j < 6; j++)
{
result[idx,j ] = Convert.ToInt32(columns[j]);
}
}
else
{
throw new Exception(string.Format("Expected 6 values on row {0},
found {1}", idx + 1, columns.Length));
}
}
}
return result;
}

I hope this partially restores your faith in these forums. It should at
least aid anyone who reads this thread to use this improved solution.
Thanks Jesse, I will implement your recommendations as I am sure
they will make my code a lot more bullet proof.
Cheers Mick.
 
C

colin.fairbrother

Hello Michael,




Hello Michael,
Michael wrote:
Why  bother answering if you are not prepared to help?
Is it just to prove how smart you are? I asked for a snippet
or a link.
If you want to decide on the form of help you get, then hire a
consultant. I am sure you can get somebody to do it for somewhere
between 50 and 100 dollars per hour.
If you want free help, then you will have to take it as it comes.
Arne
You guys are all up yourselves! The only reason I am doing
this is to help someone else make set covering algorithms work
faster! To see if C# can do it better than VB6.0. I have spent
many hours on the task. Why? Because as well as helping it
will improve my skills.
Good bye and good riddance!
Micheal,
I'm very sorry you feel this way. And I think it is great that you're
trying to help someone. And should you have formulated your question
differently, showed what you had aready created, or asked for the
most efficient way of doing it, and provided us with the original VB
code, then we would all have dug deep for you to find what you were
looking for, but even then, we'd probably provide you with the
building blocks, good advice and a collective review on your work so
far.
It's all in the way you've worded it...
Its OK Jesse, I've solved it. With no help from the self
appointed cyber police patrolling this group. I'm fixing
the code to this in case some other poor refugee from
VB6 searches the archives from this group. They would
not be expecting so complex a task, especially with the
hand grenade of double spaces between some of the .txt
data.
// Snippet for reading integers from .txt into a 2 d array
int[,] theLines = new int[163, 6]; //holds 163*6 elements
string path = @"C:\Documents and
Settings\Michael\Desktop\Wheel.txt";
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate,
FileAccess.Read));
int idx = 0;//
while (idx<163)
//THERE ARE DOUBLE SPACES BETWEEN SOME NUMBERS IN .txt
{
string row = textIn.ReadLine();
row = row.Trim();
row = row.Replace("  "," ");//get rid of double spaces
string[] columns = row.Split(' ');//redeclare in case
size changes
for (int j = 0; j < 6; j++)
theLines[idx,j ] = Convert.ToInt32(columns[j]);
idx++;

//Check all is well
for (int i = 0; i < 163; i++)
for (int j = 0; j < 6; j++)
{
Console.WriteLine(theLines[i, j]);
}
textIn.Close();

You can simplify your double space prblem by using the

String::Split Method (String[], StringSplitOptions)

overload. Seehttp://msdn.microsoft.com/en-us/library/tabh47cf.aspx. Like so:

row.Split(new string[]{"  ", " "}, StringSplitOptions.None);

Or you could use the StringSplitOptions.RemoveEmptyEntries like this:

row.Split(' ', StringSplitOptions.RemoveEmptyEntries);

This last option should negate the need to use row.Trim as well.

Another thing to note, is that it would be best to use a using(Disposable
object){} to ensure proper cleanup of your stream objects

I also added some error checking here and there...

public int[,] ParseFile(string path)
{
  int[,] result= new int[163, 6]; //holds 163*6 elements

  if (!File.Exists(path))
  {
     throw new FileNotFoundException();
  }

  using (StreamReader textIn = new StreamReader(new FileStream(path, FileMode.Open,
FileAccess.Read)))
  {
    while (int idx = 0; idx < 163; idx++)
    {
      if (textIn.Peak() == -1) // check for end-of-file
      {
        throw new Exception(string.Format("Unexpected end of fileat line
{0}", idx + 1));
      }

      string row = textIn.ReadLine();
      string[] columns = row.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// StringSplitOptions removed double spaces and trims values.

      if (colums.Length == 6) // check expected input
        for (int j = 0; j < 6; j++)
        {
          result[idx,j ] = Convert.ToInt32(columns[j]);
        }
      }
      else
      {
         throw new Exception(string.Format("Expected 6 values on row {0},
found {1}", idx + 1, columns.Length));
      }
    }
  }
  return result;

}

I hope this partially restores your faith in these forums. It should at least
aid anyone who reads this thread to use this improved solution.
--
Jesse Houwing
jesse.houwing at sogeti.nl- Hide quoted text -

- Show quoted text -

Jesse

Thanks for that - I was having the same problem with a mixture of
single and double spacing.

I came to this group by looking up Michael's profile to have a peek at
where he was asking questions. I hope the other members of this group
could maybe think about doing the same before jumping on someone.
Sure, the internet is full of people looking for someone else to do
their homework but that was not the case here. I doubt any of you, no
matter how brilliant, have not got stuck at some time and just stating
the problem in a group such as this can be catalyst enough to solving
it before looking at someone else's answer.

Michael has been selflessly providing Coverage Calculation code and I
have found it very interesting to see how someone else goes about
tackling tasks I've already addressed albeit in a slower way. I've
learnt some new tricks from Michael and I'm not too proud to admit it.

You may care to go slumming in the unmoderated newsgroup and see what
he has done, perhaps offer a suggestion or two. For the code provided
in VB6 I tried it out in C++ and C# and found VB 2008 to be faster.

http://groups.google.com/group/rec.gambling.lottery/browse_thread/thread/0c60afe7b167d2d2#

http://groups.google.com/group/rec.gambling.lottery/browse_thread/thread/396c92e7bf9a0242#

Gentlemen
Keep Smiling

Colin Fairbrother

www.lottoposter.com

ps You may care to wonder why groups are dominated by men.
 
C

colin.fairbrother

Hello Michael,




Hello Michael,
Michael wrote:
Why  bother answering if you are not prepared to help?
Is it just to prove how smart you are? I asked for a snippet
or a link.
If you want to decide on the form of help you get, then hire a
consultant. I am sure you can get somebody to do it for somewhere
between 50 and 100 dollars per hour.
If you want free help, then you will have to take it as it comes.
Arne
You guys are all up yourselves! The only reason I am doing
this is to help someone else make set covering algorithms work
faster! To see if C# can do it better than VB6.0. I have spent
many hours on the task. Why? Because as well as helping it
will improve my skills.
Good bye and good riddance!
Micheal,
I'm very sorry you feel this way. And I think it is great that you're
trying to help someone. And should you have formulated your question
differently, showed what you had aready created, or asked for the
most efficient way of doing it, and provided us with the original VB
code, then we would all have dug deep for you to find what you were
looking for, but even then, we'd probably provide you with the
building blocks, good advice and a collective review on your work so
far.
It's all in the way you've worded it...
Its OK Jesse, I've solved it. With no help from the self
appointed cyber police patrolling this group. I'm fixing
the code to this in case some other poor refugee from
VB6 searches the archives from this group. They would
not be expecting so complex a task, especially with the
hand grenade of double spaces between some of the .txt
data.
// Snippet for reading integers from .txt into a 2 d array
int[,] theLines = new int[163, 6]; //holds 163*6 elements
string path = @"C:\Documents and
Settings\Michael\Desktop\Wheel.txt";
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate,
FileAccess.Read));
int idx = 0;//
while (idx<163)
//THERE ARE DOUBLE SPACES BETWEEN SOME NUMBERS IN .txt
{
string row = textIn.ReadLine();
row = row.Trim();
row = row.Replace("  "," ");//get rid of double spaces
string[] columns = row.Split(' ');//redeclare in case
size changes
for (int j = 0; j < 6; j++)
theLines[idx,j ] = Convert.ToInt32(columns[j]);
idx++;

//Check all is well
for (int i = 0; i < 163; i++)
for (int j = 0; j < 6; j++)
{
Console.WriteLine(theLines[i, j]);
}
textIn.Close();

You can simplify your double space prblem by using the

String::Split Method (String[], StringSplitOptions)

overload. Seehttp://msdn.microsoft.com/en-us/library/tabh47cf.aspx. Like so:

row.Split(new string[]{"  ", " "}, StringSplitOptions.None);

Or you could use the StringSplitOptions.RemoveEmptyEntries like this:

row.Split(' ', StringSplitOptions.RemoveEmptyEntries);

This last option should negate the need to use row.Trim as well.

Another thing to note, is that it would be best to use a using(Disposable
object){} to ensure proper cleanup of your stream objects

I also added some error checking here and there...

public int[,] ParseFile(string path)
{
  int[,] result= new int[163, 6]; //holds 163*6 elements

  if (!File.Exists(path))
  {
     throw new FileNotFoundException();
  }

  using (StreamReader textIn = new StreamReader(new FileStream(path, FileMode.Open,
FileAccess.Read)))
  {
    while (int idx = 0; idx < 163; idx++)
    {
      if (textIn.Peak() == -1) // check for end-of-file
      {
        throw new Exception(string.Format("Unexpected end of fileat line
{0}", idx + 1));
      }

      string row = textIn.ReadLine();
      string[] columns = row.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// StringSplitOptions removed double spaces and trims values.

      if (colums.Length == 6) // check expected input
        for (int j = 0; j < 6; j++)
        {
          result[idx,j ] = Convert.ToInt32(columns[j]);
        }
      }
      else
      {
         throw new Exception(string.Format("Expected 6 values on row {0},
found {1}", idx + 1, columns.Length));
      }
    }
  }
  return result;

}

I hope this partially restores your faith in these forums. It should at least
aid anyone who reads this thread to use this improved solution.
--
Jesse Houwing
jesse.houwing at sogeti.nl- Hide quoted text -

- Show quoted text -

Jesse

Thanks for that - I was having the same problem with a mixture of
single and double spacing.

I came to this group by looking up Michael's profile to have a peek at
where he was asking questions. I hope the other members of this group
could maybe think about doing the same before jumping on someone.
Sure, the internet is full of people looking for someone else to do
their homework but that was not the case here. I doubt any of you, no
matter how brilliant, have not got stuck at some time and just stating
the problem in a group such as this can be catalyst enough to solving
it before looking at someone else's answer.

Michael has been selflessly providing Coverage Calculation code and I
have found it very interesting to see how someone else goes about
tackling tasks I've already addressed albeit in a slower way. I've
learnt some new tricks from Michael and I'm not too proud to admit it.

You may care to go slumming in the unmoderated newsgroup and see what
he has done, perhaps offer a suggestion or two. For the code provided
in VB6 I tried it out in C++ and C# and found VB 2008 to be faster.

http://groups.google.com/group/rec.gambling.lottery/browse_thread/thread/0c60afe7b167d2d2#

http://groups.google.com/group/rec.gambling.lottery/browse_thread/thread/396c92e7bf9a0242#

Gentlemen
Keep Smiling

Colin Fairbrother

www.lottoposter.com

ps You may care to wonder why groups are dominated by men.
 
C

Cor Ligthert[MVP]

Colin,

Your profile tells you are from Sydney like Michael.

What a coincidence.

Cor
 
C

Cor Ligthert[MVP]

Colin,

Your profile tells you are from Sydney like Michael.

What a coincidence.

Cor
 
C

colin.fairbrother

[...]
I came to this group by looking up Michael's profile to have a peek at
where he was asking questions. I hope the other members of this group
could maybe think about doing the same before jumping on someone.

What are you talking about?

The only person here who "jumped on" anyone was Michael.  Even after  
Michael was rude, no one did anything other than explain why his response 
was rude and why the original reply from Pavel was as helpful and useful  
as one should expect, given a question of the form Michael provided.

Regardless, most of us are reading this newsgroup via a Usenet news  
server.  There's no "profile" to look at, nor should a person's profile 
make any difference with respect to whether they are "jumped on" or not.  
Michael should not have jumped on Pavel, with or without Pavel having a  
profile to view.
[...]
Michael has been selflessly providing Coverage Calculation code and I
have found it very interesting to see how someone else goes about
tackling tasks I've already addressed albeit in a slower way. I've
learnt some new tricks from Michael and I'm not too proud to admit it.

It's possible that Michael, being in a position where for whatever reason 
he is simply writing code for someone else, incorrectly assumed that in  
this forum, the goal is for others to simply write code for people asking 
questions.

But, having incorrect expectations is no excuse for his rude response.

Pete

Hmm

I'm starting to think Michael was right for some members of this
group. As for the Sydney bit - pure coincidence, never met him. Think
he has a bit of the brashness associated with youth but the meak and
mild generally get trodden on.

As the Americans like to say, "Have a nice day!" whether they mean it
or not. When an Australian says it he usually means the opposite.

I'm sure there are a few good souls in this group who are intrigued by
what it's all about. It's simply a bit of code to achieve something in
the fastest time. The undoubted master is John Rawson who does it in
0.2 sec whether the set has 1 block or 163 in C++. There is absolutely
nothing commercial about this - totally recreational. However, I must
warn you combinatorics is addictive, so says Tony Crilly.

You can post your better solution here in C# of course.
http://groups.google.com/group/lottogroup?lnk=

Colin Fairbrother
 
C

colin.fairbrother

[...]
I came to this group by looking up Michael's profile to have a peek at
where he was asking questions. I hope the other members of this group
could maybe think about doing the same before jumping on someone.

What are you talking about?

The only person here who "jumped on" anyone was Michael.  Even after  
Michael was rude, no one did anything other than explain why his response 
was rude and why the original reply from Pavel was as helpful and useful  
as one should expect, given a question of the form Michael provided.

Regardless, most of us are reading this newsgroup via a Usenet news  
server.  There's no "profile" to look at, nor should a person's profile 
make any difference with respect to whether they are "jumped on" or not.  
Michael should not have jumped on Pavel, with or without Pavel having a  
profile to view.
[...]
Michael has been selflessly providing Coverage Calculation code and I
have found it very interesting to see how someone else goes about
tackling tasks I've already addressed albeit in a slower way. I've
learnt some new tricks from Michael and I'm not too proud to admit it.

It's possible that Michael, being in a position where for whatever reason 
he is simply writing code for someone else, incorrectly assumed that in  
this forum, the goal is for others to simply write code for people asking 
questions.

But, having incorrect expectations is no excuse for his rude response.

Pete

Hmm

I'm starting to think Michael was right for some members of this
group. As for the Sydney bit - pure coincidence, never met him. Think
he has a bit of the brashness associated with youth but the meak and
mild generally get trodden on.

As the Americans like to say, "Have a nice day!" whether they mean it
or not. When an Australian says it he usually means the opposite.

I'm sure there are a few good souls in this group who are intrigued by
what it's all about. It's simply a bit of code to achieve something in
the fastest time. The undoubted master is John Rawson who does it in
0.2 sec whether the set has 1 block or 163 in C++. There is absolutely
nothing commercial about this - totally recreational. However, I must
warn you combinatorics is addictive, so says Tony Crilly.

You can post your better solution here in C# of course.
http://groups.google.com/group/lottogroup?lnk=

Colin Fairbrother
 
C

colin.fairbrother

I'm starting to think Michael was right for some members of this
group. [...]

And I'm starting to think you haven't even bothered to read any of the  
messages in this thread.

Michael is the only person who has posted anything even remotely  
inflammatory.  I don't know exactly what you think Michael "was right"  
about, but if by that you mean he was right that no one here was willing  
to just write his code for him, guilty as charged.  But what obligationdo  
any of us have to just up and do the work for any random person who shows 
up here?  It's certainly nothing for you or Michael to get your shorts in  
a twist about.

Pete

The answer to the question of why not many women are represented in
these groups is that some of the "men" are just too girlie, girlie.
(Sorry Gov.) Now, go and get hysterical with one of your dolls. No
spanking though, dolls have rights!

Colin is leaving the building.
 
C

colin.fairbrother

I'm starting to think Michael was right for some members of this
group. [...]

And I'm starting to think you haven't even bothered to read any of the  
messages in this thread.

Michael is the only person who has posted anything even remotely  
inflammatory.  I don't know exactly what you think Michael "was right"  
about, but if by that you mean he was right that no one here was willing  
to just write his code for him, guilty as charged.  But what obligationdo  
any of us have to just up and do the work for any random person who shows 
up here?  It's certainly nothing for you or Michael to get your shorts in  
a twist about.

Pete

The answer to the question of why not many women are represented in
these groups is that some of the "men" are just too girlie, girlie.
(Sorry Gov.) Now, go and get hysterical with one of your dolls. No
spanking though, dolls have rights!

Colin is leaving the building.
 
C

colin.fairbrother

Huh?  Women avoid the newsgroups because too many people in the newsgroups  
act like women?

No wonder you need help with your code.

I don't need help with my code; I do it myself even if it takes
longer. This way I don't have to put up with naive, over-reactive
people who try to hide their inadequacies with a strident outburst of
frothy, banelness. Your attributing of wants, views etc to others
shows an illogicalness that cannot be reconciled with a competent
programmer.

There is not one line of code in this thread by you; why do I think
that is pretty normal for your involvement in this group?

Being such an immature chappy I realize you have to have the last
word. I came to this group by accident; I leave it with thanks to
Jesse and an advice that your puerile expected reply will not be
viewed by me.

Colin Fairbrother
www.lottoposter.com
www.lottotowin.com
 
C

colin.fairbrother

Huh?  Women avoid the newsgroups because too many people in the newsgroups  
act like women?

No wonder you need help with your code.

I don't need help with my code; I do it myself even if it takes
longer. This way I don't have to put up with naive, over-reactive
people who try to hide their inadequacies with a strident outburst of
frothy, banelness. Your attributing of wants, views etc to others
shows an illogicalness that cannot be reconciled with a competent
programmer.

There is not one line of code in this thread by you; why do I think
that is pretty normal for your involvement in this group?

Being such an immature chappy I realize you have to have the last
word. I came to this group by accident; I leave it with thanks to
Jesse and an advice that your puerile expected reply will not be
viewed by me.

Colin Fairbrother
www.lottoposter.com
www.lottotowin.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top