comp.lang.ada
 help / color / mirror / Atom feed
* unbound array?
@ 2004-07-26  2:20 John Clarke
  2004-07-26  3:10 ` Jim Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: John Clarke @ 2004-07-26  2:20 UTC (permalink / raw)


Hello!

I was hoping someone could shed some light. I am writing an ADA program that
reads a file and counts the number of words in the text file and stores the
word length, of each word encountered, in an array. So for instance i have
defined:

size : integer := 100;
type length_type is array (1..size) of integer;
word_length : length_type;

Now, if the text file is: "A rat was here."

Hence, word_length(1) = 1, word_length(2) = 3, word_length(4) = 4 etc...

The problem I am facing is that I don't know how many words I will encounter
in the text file. I do not want to constrain size to 100 as I have above. I
know I can use unconstrained array type, but this also doesn't solve the
problem as an unconstrained array requires a predefined size assigned prior
to assigning values to it.

I know that size will never be greater than the number of characters in the
text file. But it would be a little ridiculous assigning "size" = "number of
characters in text file".

How can this problem be solved? Any help would be great.

Thanks
Peter





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: unbound array?
  2004-07-26  2:20 unbound array? John Clarke
@ 2004-07-26  3:10 ` Jim Rogers
  2004-07-26  5:43 ` tmoran
  2004-07-26 11:37 ` Martin Krischik
  2 siblings, 0 replies; 5+ messages in thread
From: Jim Rogers @ 2004-07-26  3:10 UTC (permalink / raw)


"John Clarke" <jclarke@abit.com.au> wrote in
news:41046a70@dnews.tpgi.com.au: 

> Hello!
> 
> I was hoping someone could shed some light. I am writing an ADA
> program that reads a file and counts the number of words in the text
> file and stores the word length, of each word encountered, in an
> array. So for instance i have defined:
> 
> size : integer := 100;
> type length_type is array (1..size) of integer;
> word_length : length_type;
> 
> Now, if the text file is: "A rat was here."
> 
> Hence, word_length(1) = 1, word_length(2) = 3, word_length(4) = 4
> etc... 
> 
> The problem I am facing is that I don't know how many words I will
> encounter in the text file. I do not want to constrain size to 100 as
> I have above. I know I can use unconstrained array type, but this also
> doesn't solve the problem as an unconstrained array requires a
> predefined size assigned prior to assigning values to it.
> 
> I know that size will never be greater than the number of characters
> in the text file. But it would be a little ridiculous assigning "size"
> = "number of characters in text file".
> 
> How can this problem be solved? Any help would be great.
> 

Does the result have to be stored in an array?
Can it be stored in a list instead?

Jim Rogers




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: unbound array?
  2004-07-26  2:20 unbound array? John Clarke
  2004-07-26  3:10 ` Jim Rogers
@ 2004-07-26  5:43 ` tmoran
  2004-07-26 11:37 ` Martin Krischik
  2 siblings, 0 replies; 5+ messages in thread
From: tmoran @ 2004-07-26  5:43 UTC (permalink / raw)


>The problem I am facing is that I don't know how many words I will encounter
>in the text file. I do not want to constrain size to 100 as I have above. I

> But it would be a little ridiculous assigning "size" = "number of
> characters in text file".
   If "words" must be separated by blanks or other punctuation, then
size := size of text file/2 will always do.
   If it's real text, with a known statistical word length distribution,
you can use
size = a large but reasonable size + file_size/(avg_word_and_punctuation_size)
and, by choosing the first parameter you can make it arbitrarily
improbable that the data will overflow your array.

A q&d approach for small files could use recursion:

 type word_lengths is range 1 .. whatever is reasonable

 type length_type is array (integer range <>) of word_lengths;

 function add_a_count(current_list : length_type) return length_type is
 begin
   if end of file then
     return current_list;
   else
     return add_a_count(current_list & (read_next_length));
   end if;
 end add_a_count;

 null_length : length_type(1 .. 0);
 word_length : length_type := add_a_count(null_length);

Perhaps more practical would be to do it in chunks of some suitable
size, recursing only if the current chunk became full.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: unbound array?
  2004-07-26  2:20 unbound array? John Clarke
  2004-07-26  3:10 ` Jim Rogers
  2004-07-26  5:43 ` tmoran
@ 2004-07-26 11:37 ` Martin Krischik
  2004-07-28 14:09   ` zork
  2 siblings, 1 reply; 5+ messages in thread
From: Martin Krischik @ 2004-07-26 11:37 UTC (permalink / raw)


John Clarke wrote:

> Hello!
> 
> I was hoping someone could shed some light. I am writing an ADA program
> that reads a file and counts the number of words in the text file and
> stores the word length, of each word encountered, in an array. So for
> instance i have defined:

> How can this problem be solved? Any help would be great.

If it is not teaching then I suggest to use a container library. See
www.adaworld.com for a nice collection of different container libs.

If it is teaching: Still get a container lib and look how they solved the
problem ;-).

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: unbound array?
  2004-07-26 11:37 ` Martin Krischik
@ 2004-07-28 14:09   ` zork
  0 siblings, 0 replies; 5+ messages in thread
From: zork @ 2004-07-28 14:09 UTC (permalink / raw)


Thanks everyone for your responses. I have decided to resort to using a
linked list. It works nicely and is fairly compact in size. :)

cheers,
zork





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-07-28 14:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-26  2:20 unbound array? John Clarke
2004-07-26  3:10 ` Jim Rogers
2004-07-26  5:43 ` tmoran
2004-07-26 11:37 ` Martin Krischik
2004-07-28 14:09   ` zork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox