comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: unbound array?
Date: Mon, 26 Jul 2004 05:43:07 GMT
Date: 2004-07-26T05:43:07+00:00	[thread overview]
Message-ID: <LR0Nc.163873$IQ4.146435@attbi_s02> (raw)
In-Reply-To: 41046a70@dnews.tpgi.com.au

>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.



  parent reply	other threads:[~2004-07-26  5:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-26  2:20 unbound array? John Clarke
2004-07-26  3:10 ` Jim Rogers
2004-07-26  5:43 ` tmoran [this message]
2004-07-26 11:37 ` Martin Krischik
2004-07-28 14:09   ` zork
replies disabled

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