Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accumulating data from a file

0 views
Skip to first unread message

ashley.f...@gmail.com

unread,
Oct 8, 2008, 10:59:37 AM10/8/08
to
Suppose I have a file of the format:

A 1,3,7
B 4,3,9
A 7,4,5
...

What I need to do is produce output of the form:

A 1,3,7,4,5
B 4,3,9,

i.e. a list of unique #'s for each character. I figure that the best
way to parse this file would be to read a line at a time & use a DCG
to extract the character & numbers from each line. After I have
these, I need to accumulate these lists of numbers for each character
to print out after I'm done w/ the file. What's the recommended data
structure for this? In an imperative programming language, I'd use a
hashtable. For Prolog, one idea that comes to mind is a list of
pairs:

[ p('A', [ 1, 3, 7, 4, 5 ]), p('B', [ 4, 3, 9 ]) ]

I'm assuming I'd have to open up this list & reinsert an updated pair
for each character for which I find a line in the file as I'm parsing
the file. Any other suggestions?

Thanks in advance.

bart demoen

unread,
Oct 8, 2008, 12:03:05 PM10/8/08
to
On Wed, 08 Oct 2008 07:59:37 -0700, ashley.fernandes wrote:


> For Prolog, one idea that comes to mind is a list of pairs:
>
> [ p('A', [ 1, 3, 7, 4, 5 ]), p('B', [ 4, 3, 9 ]) ]
>
> I'm assuming I'd have to open up this list & reinsert an updated pair
> for each character for which I find a line in the file as I'm parsing
> the file. Any other suggestions?

Yes - keep the lists ordered. But it is not essential.
Apart from that: go ahead with your plan. You will learn quite a bit of
Prolog while doing so.

And don't forget to post your code here !

Cheers

Bart Demoen

0 new messages