[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Aug 21 14:29:39 1996 
Subject:Re: If anyone is there - File I/O 
From:Ian Rogers 
Volume-ID:960822.01 

Jon Timmis (jit3@aber.ac.uk) wrote:
> Hi to anyone reading this group,

> I am having problems reading in lists from a file.  I need to be able
> to read in one list at a time, do some processing then continue reading
> in the file, these lists are separated by a blankline.
> The problem I have with my solutions is as follows :

> discin(filename) -> char_cons;
> incharitem(char_cons) -> item_cons;

> repeat
>    item_cons() -> the_char;
>    quitif(the_char == termin);
>    the_char :: the_result -> the_result;
>    if (the_char == newline) then
>       pr('end of list'); pr(newline);
>       rev(the_result) -> TC_the_data;
>       construct_list() -> result;
>       result ==>
>       [] -> result;
>       [] -> the_result;
>    endif;

> endrepeat;
> As you may be aware, all whitespaces are ignorded with this method, so
> checking
> for a newline is a waste of time (any advice here would be welcomed).

See REF * POPNEWLINE

I'd code it something like this (but I'm just typing this in off the top
of my head - so read, understand and correct it before use ;-)

    define get_next_list(itemrep);
        lvars   itemrep, item,
                previous = false,
            ;
        dlocal popnewline = true;
        [%  repeat
                itemrep() -> item;
                quitif(item == termin); ;;; we're at the end of the file

                ;;; if we get two newlines in a row then we're done
                quitif(item == newline and previous == newline);

                item -> previous;

                ;;; if item is not a newline then drop it into the list
                unless item == newline do item endunless;
            endrepeat
        %]
    enddefine;


and you'd use it like

    vars char_cons, item_cons, list;
    discin(filename) -> char_cons;
    incharitem(char_cons) -> item_cons;
    until (get_next_list(item_cons) ->> list) == [] do
        process_one_list(list);
    enduntil;

where process_one_list does the processing you mentioned.


Ian.