Hi my split still doesn't seem to be working correctly i have now
specified it to split by s.Split(' ',',','.',';'); but now i am
getting more words than are actually there, i will post the input
file, and hope you can point me in he right direction on how i should
split. Also I am still getting duplicates in the output file, any idea
what this could be? I will post a sample of this many thanks.
Input.txt:
No one would have believed in the last years of the nineteenth
century that this world was being watched keenly and closely by
intelligences greater than man's and yet as mortal as his own; that as
men busied themselves about their various concerns they were
scrutinised and studied, perhaps almost as narrowly as a man with a
microscope might scrutinise the transient creatures that swarm and
multiply in a drop of water. With infinite complacency men went to
and fro over this globe about their little affairs, serene in their
assurance of their empire over matter. It is possible that the
infusoria under the microscope do the same. No one gave a thought to
the older worlds of space as sources of human danger, or thought of
them only to dismiss the idea of life upon them as impossible or
improbable. It is curious to recall some of the mental habits of
those departed days. At most terrestrial men fancied there might be
other men upon Mars, perhaps inferior to themselves and ready to
welcome a missionary enterprise. Yet across the gulf of space, minds
that are to our minds as ours are to those of the beasts that perish,
intellects vast and cool and unsympathetic, regarded this earth with
envious eyes, and slowly and surely drew their plans against us. And
early in the twentieth century came the great disillusionment.
The planet Mars, I scarcely need remind the reader, revolves about the
sun at a mean distance of 140,000,000 miles, and the light and heat it
receives from the sun is barely half of that received by this world.
It must be, if the nebular hypothesis has any truth, older than our
world; and long before this earth ceased to be molten, life upon its
surface must have begun its course. The fact that it is scarcely one
seventh of the volume of the earth must have accelerated its cooling
to the temperature at which life could begin. It has air and water
and all that is necessary for the support of animated existence.
Yet so vain is man, and so blinded by his vanity, that no writer,
up to the very end of the nineteenth century, expressed any idea that
intelligent life might have developed there far, or indeed at all,
beyond its earthly level. Nor was it generally understood that since
Mars is older than our earth, with scarcely a quarter of the
superficial area and remoter from the sun, it necessarily follows that
it is not only more distant from time's beginning but nearer its end.
The secular cooling that must someday overtake our planet has
already gone far indeed with our neighbour. Its physical condition is
still largely a mystery, but we know now that even in its equatorial
region the midday temperature barely approaches that of our coldest
winter. Its air is much more attenuated than ours, its oceans have
shrunk until they cover but a third of its surface, and as its slow
seasons change huge snowcaps gather and melt about either pole and
periodically inundate its temperate zones. That last stage of
exhaustion, which to us is still incredibly remote, has become a
present-day problem for the inhabitants of Mars. The immediate
pressure of necessity has brightened their intellects, enlarged their
powers, and hardened their hearts. And looking across space with
instruments, and intelligences such as we have scarcely dreamed of,
they see, at its nearest distance only 35,000,000 of miles sunward of
them, a morning star of hope, our own warmer planet, green with
vegetation and grey with water, with a cloudy atmosphere eloquent of
fertility, with glimpses through its drifting cloud wisps of broad
stretches of populous country and narrow, navy-crowded seas.
And we men, the creatures who inhabit this earth, must be to them
at least as alien and lowly as are the monkeys and lemurs to us. The
intellectual side of man already admits that life is an incessant
struggle for existence, and it would seem that this too is the belief
of the minds upon Mars. Their world is far gone in its cooling and
this world is still crowded with life, but crowded only with what they
regard as inferior animals. To carry warfare sunward is, indeed,
their only escape from the destruction that, generation after
generation, creeps upon them.
And before we judge of them too harshly we must remember what
ruthless and utter destruction our own species has wrought, not only
upon animals, such as the vanished bison and the dodo, but upon its
inferior races. The Tasmanians, in spite of their human likeness,
were entirely swept out of existence in a war of extermination waged
by European immigrants, in the space of fifty years. Are we such
apostles of mercy as to complain if the Martians warred in the same
spirit?
The Martians seem to have calculated their descent with amazing
subtlety--their mathematical learning is evidently far in excess of
ours--and to have carried out their preparations with a well-nigh
perfect unanimity.
Output.txt:
1
1
The 1
and 1
at 1
beyond 1
came 1
fluctuating 1
flying 1
indicated 1
intellects 1
It 1
made 1
Ogilvy 1
People 1
softened 1
their 1
they 1
three 1
up 1
visible 1
were 1
with 1
" 1
"
1
"as
flaming 1
"The 1
(the 1
000 1
140 1
1894 1
2 1
35 1
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A
flame 1
A
heavy 1
A
microscope 1
A
party 1
A
present-day 1
A
scrutiny 1
About 8
About 8
About 8
"Bob Grommes" <bo*@bobgrommes.com> wrote in message news:<Oh**************@TK2MSFTNGP09.phx.gbl>...
Split() is a very simplistic method. It can split on a single character, or
on multiple characters (have a look at the Split(char[]) override) but if
you have for instance quoted strings within which delimiters must be
ignored, as with:
Fred,Smith,"Stockton, MD"
... or other special cases, the you'll have to write your own routine using
string manipulation methods and/or Regex.
Your code looks fine offhand. If you can provide your input.txt file and a
sample of the bad output that would be helpful in diagnosing your problem.
Incidentally, foreach does not guarantee that it will traverse a collection
in order, though in my experience it does do so with arrays and ArrayLists.
Still, before concluding that the ArrayList is not really sorted, I'd use a
for loop instead. Or look at it in the debugger.
As for capitalizing the first letter of each word -- you have to write a
method for that yourself. Simplistically, something like:
string ToProperCase(string strWord) {
if (strWord.Length == 0) {
return "";
} else if (strWord.Length == 1) {
return strWord.ToUpper();
} else {
return Char.ToUpper(strWord[0]).ToString() + strWord.Substring(1);
}
}
--Bob
"steve smith" <bo**********@hotmail.com> wrote in message
news:4b**************************@posting.google.c om... Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am using the split
function with ' ', but how do i get it to take into account
punctuation marks such as ',',',' etc?
Also I am then trying to add contents of an array of strings to an
arraylist, but only if the string isn't already there. I was using the
arraylist.contains method, but it is still adding duplicates, and
finally, I was trying to sort my arraylist, in alphabetical order
using arraylist.sort(), but that doesn't seem to sort it fully, any
ideas on where i am going wrong, i have posted my code below. One
final question, before i add my strings of words to my arraylist, is
it possible to capitalise the first letter in each word? Thanks.
public static void ParseFile(string input, string output) {
System.IO.TextReader r = System.IO.File.OpenText(@"C:\input.txt");
System.IO.TextWriter w = System.IO.File.CreateText(@"C:\output.txt");
string s = r. ReadToEnd();
ArrayList myList = new ArrayList();
int numOccur = 0;
string[] mySplit = s.Split(' ');
Console.WriteLine("Num of words is " + mySplit.Length);
for(int x = 0; x < mySplit.Length; x++) {
if (!myList.Contains(mySplit[x])) {
myList.Add(mySplit[x]);
}
}
myList.Sort();
foreach (string item in myList) {
w.Write(item + "\n");
}
r.Close();
w.Close();
}