Arc Forumnew | comments | leaders | submitlogin
Reading from a file
1 point by jsgrahamus 4354 days ago | 14 comments
From a lisp/arc newbie: How does one read from a file?

Thanks, Steve



1 point by jsgrahamus 4354 days ago | link

So, I was thinking of using arc for an easy task. I have a file with a line of text, a blank line, a line of text, etc. I wanted to rewrite it without the blank lines.

Looking into the link below I found code for reading a file: arc> dev "c:\\Users\\Steve\\Desktop\\mbrm.txt" arc> (w/instring ins dev (whiler l (readline ins) nil (prn "{" l "}"))) {c:\Users\Steve\Desktop\mbrm.txt} nil arc>

However I don't see any lines read. Any ideas?

Thanks, Steve

-----

1 point by jsgrahamus 4354 days ago | link

Sorry about the formatting. I thought the key was to put 2 spaces at the front of the line.

arc> dev

"c:\\Users\\Steve\\Desktop\\mbrm.txt"

arc> (w/instring ins dev

       (whiler l (readline ins) nil

                 (prn "{" l "}")))
{c:\Users\Steve\Desktop\mbrm.txt}

nil

arc>

C:\Users\Steve\Desktop>dir mbrm*

Volume in drive C is TI105757W0A

Volume Serial Number is 48C4-C0F7

Directory of C:\Users\Steve\Desktop

04/26/2012 11:15 AM 254,785 mbrm.txt

04/26/2012 11:35 AM 242,988 mbrm2.txt

               2 File(s)        497,773 bytes

               0 Dir(s)  146,673,606,656 bytes free

C:\Users\Steve\Desktop>

-----

2 points by zck 4354 days ago | link

One note:

  (whiler line (readline file) nil ...)
can be replaced by:

  (whilet line (readline file) ...)
Read it as "while-let"; the spelling confused me for a while.

-----

1 point by jsgrahamus 4353 days ago | link

That's shorter. What are any other differences? I copied the first one from the docs and am not sure what the nil was for.

Thanks.

-----

3 points by zck 4353 days ago | link

They're very similar. Check out http://files.arcfn.com/doc/iteration.html#whilet for some better documentation, but it boils down to this:

  (whilet var test ...)
loops until (no (test)) is true -- until (test) returns 'nil. Each loop through, var is bound to the value of (test). On the other hand,

  (whiler var expr endval ...)
loops until (is (expr) endval) is true. Similarly, each loop through, var is bound to the value of (expr).

So every time you have a call to

  (whiler value (function) nil ...)
You can replace that with

  (whilet value (function) ...
An example of when you would want to use whiler is:

  (whiler input (readline) "quit" (do-stuff-with-input-from-user input))

-----

1 point by jsgrahamus 4353 days ago | link

Good explanation. Thanks.

-----

1 point by jsgrahamus 4354 days ago | link

I really should learn to read and understand before

writing....

arc> (w/infile inf dev

        (whiler l (read inf) nil (prn "{" l "}")))
Here is a sample of the output:

{Arcanobacterium}

{has}

{been}

{strongly}

{associated}

{with}

{pharyngitis}

{(fn (_) (often with the presence of scarlatiniform rash))}

Error: "c:/Users/Steve/Desktop/mbrm-new.txt::13563: read:

illegal use of \".\""

It is odd that it errored and that it is only picking up a

word at a time instead of a line.

-----

1 point by jsgrahamus 4354 days ago | link

Reading comes to the rescue again:

arc> dev

"c:/users/steve/desktop/mbrm-new.txt"

arc> (w/infile inf dev

        (whiler l (readline inf) nil (prn l)))
...

VZ Varicella zoster virus Varicella zoster virus

VZV Varicella Varicella Zoster isolated

W White White

WADA Wangiella dermatitidis Wangiella dermatitidis

WBCS WBCs seen WBCs seen

WDER Wangiella dermatitidis Wangiella dermatitidis W. dermatitidis

WESP Weeksella species Weeksella species Weeksella sp.

WEVI Weeksella virosa Weeksella virosa W. virosa

WEZO Weeksella zoohelcum Weeksella zoohelcum W. zoohelcum

WHITE White White

WNF Normal flora also recovered Normal flora also recovered

WOO The specimen was collected The specimen was collected with a wooden shafted swab. Wood components may inhibit virus isolation. Interpret these negative results with caution.

...

nil ...

-----

1 point by waterhouse 4354 days ago | link

The "read" procedure will read only a single Arc expression at a time. If you want to read a line, there is a "readline" procedure.

Also, it is correct that you make code look like code by putting two spaces before each line of code.

  After "code." in the above paragraph, there is a newline, then
  another newline, then two spaces, then "After [...] newline, then",
  then another newline, then two spaces, then "another", and so on.

-----

1 point by jsgrahamus 4354 days ago | link

See reply.

-----

1 point by jsgrahamus 4354 days ago | link

Thanks for the editing tips.

  arc> dev
  "c:/users/steve/desktop/mbrm-new.txt"
  arc> (w/infile inf dev
          (whiler l (readline inf)
             nil
             (if (no (is l "\r"))
                 (prn l))))

  ...
   
  YT      Yeast   Yeast   Yeast
  YTNC    Yeast not Cryptococcus  Yeast not Cryptococcus  Yst not Cryptococcus
  YTSP    Yeast species   Yeast species   Yeast sp.nentgd  Group D, streptococcus  Group D, streptococcus  Group D
  oi      Over-inoculated Over-inoculated Over-inoculated
  orgnv   Organism non-viable     Organism non-viable     Organism non-viable
  oxnego  Oxidase Negative Other  Oxidase Negative Other  Oxidase Negative
  p27853  Pseudomonas aeruginosa ATCC 27853       Pseudomonas aeruginosa ATCC 27853       P. aerug ATCC27853
  pacica  Presumptive Acinetobacter lwoffi        Presumptive Acinetobacter lwoffi        Presumptive A.lwoffi
  stlu    Staphylococcus lugdunensis      Staphylococcus lugdunensis (coagulase negative species) S. lugdunensis
  nil
  arc>

-----

1 point by jsgrahamus 4354 days ago | link

And, I thought there was a keyword index on the wiki. URL?

Thanks again.

-----

1 point by akkartik 4354 days ago | link

Perhaps you're thinking of http://files.arcfn.com/doc?

-----

1 point by jsgrahamus 4354 days ago | link

Yes. Thanks.

-----