Arc Forumnew | comments | leaders | submitlogin
Using Arc at work
2 points by jsgrahamus 650 days ago | 17 comments
Sometimes I need to capture what is on my screen and manipulate the data so captured. So, I will scrape what is on the screen, set up a loop in my work language, and read each line into the loop. In MUMPS it would look like this:

   KILL ARR FOR  READ !,X:15 Q:'$TRUE  S ARR($INCREMENT(ARR))=X
How would I do something similar in Arc?

Thanks, Steve



1 point by jsgrahamus 650 days ago | link

This is the line of code above slightly modified

  KILL ARR FOR I=1:1 READ !,X:5 QUIT:'$TEST  SET ARR(I)=X
(Press Shift-Insert or Ctr-V or whatever to insert buffer)

  PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG
           N ARR,FND,I,RSD,RTN,STOP,TXT
           W !!,"PASTE"
           F  R !,X:15 Q:'$T  S ARR($I(ARR))=X
           K RSDS
           S STOP=0,I=1 F  S RTN=$P(ARR(I),".") D  Q:STOP
           . F  S I=$O(ARR(I)) S:I="" STOP=1 Q:STOP  Q:ARR(I)?1.AN1".".E  D
           . . S TXT=ARR(I),FND=$F(TXT,"RSD ")
           . . I FND D
           . . . S RSD=$P($E(TXT,FND,999)," ")
           . . . S RSDS(RSD,RTN_"^"_$P(TXT,"+"))=""
           W !!
           S RSD=""
           F  S RSD=$O(RSDS(RSD)) Q:RSD=""  D
           . W !!,RSD
           . S RTN=""
           . F  S RTN=$O(RSDS(RSD,RTN)) Q:RTN=""  D
           . . W !?5,RTN
           Q
           ;
  FIND     ;
           N CTR,I,RTN,STR,TAG,TXT
           R !!,"STRING: ",STR Q:STR=""
           R !,"ROUTINE: ",RTN Q:RTN=""
           F I=1:1 S TXT=$T(+I^@RTN) Q:TXT=""  D
           . I TXT?1(1AN,1"%").E D
           . . S TAG=$P($P($P(TXT," "),$C(9)),"("),CTR=0
           . E  D
           . . S CTR=CTR+1
           . I TXT[STR W !,TAG W:CTR "+",CTR W ?14,TXT
           Q
           ;
Entering the next line will show the contents of the array:

  ZWRITE ARR

  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"
  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"
  ARR(3)="         W !!,"PASTE""
  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"
  ARR(5)="         K RSDS"
  ARR(6)="         S STOP=0,I=1 F  S RTN=$P(ARR(I),".") D  Q:STOP"
  ARR(7)="         . F  S I=$O(ARR(I)) S:I="" STOP=1 Q:STOP  Q:ARR(I)?1.AN1".".E  D"
  ARR(8)="         . . S TXT=ARR(I),FND=$F(TXT,"RSD ")"
  ARR(9)="         . . I FND D"
  ARR(10)="         . . . S RSD=$P($E(TXT,FND,999)," ")"
  ARR(11)="         . . . S RSDS(RSD,RTN_"^"_$P(TXT,"+"))="""
  ARR(12)="         W !!"
  ARR(13)="         S RSD="""
  ARR(14)="         F  S RSD=$O(RSDS(RSD)) Q:RSD=""  D"
  ARR(15)="         . W !!,RSD"
  ARR(16)="         . S RTN="""
  ARR(17)="         . F  S RTN=$O(RSDS(RSD,RTN)) Q:RTN=""  D"
  ARR(18)="         . . W !?5,RTN"
  ARR(19)="         Q"
  ARR(20)="         ;"
  ARR(21)="FIND     ;"
  ARR(22)="         N CTR,I,RTN,STR,TAG,TXT"
  ARR(23)="         R !!,"STRING: ",STR Q:STR="""
  ARR(24)="         R !,"ROUTINE: ",RTN Q:RTN="""
  ARR(25)="         F I=1:1 S TXT=$T(+I^@RTN) Q:TXT=""  D"
  ARR(26)="         . I TXT?1(1AN,1"%").E D"
  ARR(27)="         . . S TAG=$P($P($P(TXT," "),$C(9)),"("),CTR=0"
  ARR(28)="         . E  D"
  ARR(29)="         . . S CTR=CTR+1"
  ARR(30)="         . I TXT[STR W !,TAG W:CTR "+",CTR W ?14,TXT"
  ARR(31)="         Q"
  ARR(32)="         ;"
This is the functionality I wish to clone. Doesn't amtter to me if lists are used instead of arrays.

-----

2 points by akkartik 650 days ago | link

I'm still not following what your program does. (Can you describe its inputs and how it transforms them in english?)

I remember you asked a similar question last year: http://arclanguage.org/item?id=19109. Perhaps it would help to connect up how your question here relates to that thread?

Here's how you run the code in that thread for reading lines from a file with anarki:

  $ cat x
  ab
  cd ef
  ghi

  $ cat x.arc 
  (write:w/infile file "x"
    (drain (readline file)))

  $ ./arc x.arc
  ("ab" "cd ef" "ghi")
  #t

-----

2 points by jsgrahamus 649 days ago | link

The program will read lines of data from stdin and print each of them.

Thanks for the reminder.

How does one do, what you posted, when the input is from the keyboard buffer and not from a file?

-----

1 point by akkartik 649 days ago | link

Great! Then you should be able to drop the outer w/infile form and just say:

  (drain (readline stdin))

-----

2 points by jsgrahamus 649 days ago | link

  Error: "read-char: expects argument of type <input-port>; given #<procedure:current-input-port>"

-----

1 point by akkartik 649 days ago | link

Sorry, typed that out on my phone without trying it out. You need parens around stdin.

  (drain (readline (stdin)))
Alternatively:

  (write:drain:readline:stdin)

-----

2 points by jsgrahamus 649 days ago | link

  Unknown or expired link.
In the example above the READ X:5 or 15, allowed the read 5 or 15 seconds before timing out. I wonder if that is what we are seeing here.

Thanks for the help.

-----

2 points by jsgrahamus 649 days ago | link

Here is a follow on problem as I'm going through the tutorial: obj does not work and the error message seems to access memory not involved with the obj.

  arc> (printlst alist)

  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"



  ""
  arc> (= codes (obj "Boston" 'bos "San Francisco" 'sfo "Paris" 'cdg))
  Error: "list-ref: contract violation\n  expected: exact-nonnegative-integer?\n  given: '(((codes (obj \"Boston\" (quote bos . nil) \"San Francisco\" (quote sfo . nil) \"Paris\" (quote cdg . nil) . nil) . nil) . nil))\n  argument position: 2nd\n  other arguments...:\n   '(\"\\nARR(1)=\\\"PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG\\\"\" \"\\nARR(2)=\\\"         N ARR,FND,I,RSD,RTN,STOP,TXT\\\"\" \"\\nARR(3)=\\\"         W !!,\\\"PASTE\\\"\\\"\" \"\\nARR(4)=\\\"         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X\\\"\" \"\\nARR(5)=\\\"         K RSDS\\..."
  arc>

-----

2 points by rocketnia 649 days ago | link

Judging by that error message, it looks like the variable "=" or one of its dependencies might have been reassigned somewhere along the line. The second argument in that error message indicates that = is getting hold of your read-in data somehow, so it might be something you've defined for processing this data.

The dependencies of = include expand=list, expand=, map, pair, and setforms (among others), so if any of these has been overwritten, it might do something like what you're seeing.

By the way, I think if you're not using Anarki, there's a known bug in (readline ...) where it will spuriously combine each empty line with the following line (https://sites.google.com/site/arclanguagewiki/arc-3_1/known-...). Maybe this could explain the extra \n you're getting.

-----

2 points by jsgrahamus 648 days ago | link

Thank you.

-----

2 points by jsgrahamus 649 days ago | link

Followon #2

Stopped arc and restarted it and had no problems with obj

How prone is arc to memory problems?

-----

1 point by akkartik 649 days ago | link

Hmm, not sure what happened. Not sure what you mean by memory problems, but I've never seen flakiness in a session this short. Perhaps something in your earlier session was accidentally a control character or something. Keep an eye out for it and I will too.

Here's a full session I tried out on linux:

  $ arc
  arc> (def printlst (thelist) (if (is thelist nil) (prn "") (do (prn (car thelist)) (printlst (cdr thelist)))))
  #<procedure: printlst>
  arc> (def readit () (drain (readline (stdin))))
  #<procedure: readit>
  arc> (= alist (readit))
  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"
  ("" "ARR(1)=\"PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG\"" "" "ARR(2)=\"         N ARR,FND,I,RSD,RTN,STOP,TXT\"" "" "ARR(3)=\"         W !!,\"PASTE\"\"" "" "ARR(4)=\"         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X\"" "" "ARR(5)=\"         K RSDS\"")
  arc> (printlst alist)

  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"

  ""
  arc> (= codes (obj "Boston" 'bos "San Francisco" 'sfo "Paris" 'cdg))
  #hash(("Boston" . bos) ("Paris" . cdg) ("San Francisco" . sfo))
  arc>

-----

2 points by jsgrahamus 648 days ago | link

Thanks. Not sure what happened.

-----

1 point by akkartik 649 days ago | link

I don't follow. You got this message when you tried my code snippet? That seems really strange!

-----

2 points by jsgrahamus 649 days ago | link

Perhaps it was because I was using tryarc.org?

So I tried again with arc running on Racket under Linux. Here's what I found:

  arc> (def readit () (drain (readline (stdin))))
  #<procedure: readit>
  arc> (readit)
  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"

  ("\nARR(1)=\"PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG\"" "\nARR(2)=\"         N ARR,FND,I,RSD,RTN,STOP,TXT\"" "\nARR(3)=\"         W !!,\"PASTE\"\"" "\nARR(4)=\"         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X\"" "\nARR(5)=\"         K RSDS\"" "\n")
  arc> (= alist (readit))
  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"

  ("\nARR(1)=\"PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG\"" "\nARR(2)=\"         N ARR,FND,I,RSD,RTN,STOP,TXT\"" "\nARR(3)=\"         W !!,\"PASTE\"\"" "\nARR(4)=\"         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X\"" "\nARR(5)=\"         K RSDS\"" "\n")
  arc> alist
  ("\nARR(1)=\"PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG\"" "\nARR(2)=\"         N ARR,FND,I,RSD,RTN,STOP,TXT\"" "\nARR(3)=\"         W !!,\"PASTE\"\"" "\nARR(4)=\"         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X\"" "\nARR(5)=\"         K RSDS\"" "\n")
  arc> (len alist)
  6
  arc> (def printlst (thelist) (if (is thelist nil) (prn "") (do (prn (car thelist)) (printlst (cdr thelist)))))
  #<procedure: printlst>
  arc> (printlst alist)

  ARR(1)="PARSE    ; PARSE OUTPUT OF ^%RFIND INTO RSD/RTN/TAG"

  ARR(2)="         N ARR,FND,I,RSD,RTN,STOP,TXT"

  ARR(3)="         W !!,"PASTE""

  ARR(4)="         F  R !,X:15 Q:'$T  S ARR($I(ARR))=X"

  ARR(5)="         K RSDS"



  ""
  arc>                                                   
I think the issue with the \n is sending data between Windows and Linux.

I did have to key in Ctrl-D twice to actually get the function to finish reading. Is there a better way to do this?

Thanks for all of the help with this.

Steve

-----

2 points by akkartik 649 days ago | link

Ah, yes tryarc would explain it.

By "the issue with the \n" do you mean how each line has a \n at the start? How is this sending data between Windows and Linux, can you elaborate?

Yes, it's weird that you had to hit ctrl-d twice. Thanks. I'll try your example on windows later today.

Both these issues aren't happening for me on linux, so it seems likely to be a windows issue.

Edit: Hmm, I do see a leading empty list at the start when reading stdin:

  arc> (readit)
  abc
  def
  ("" "abc" "def")
It looks like stdin doesn't behave quite like a regular file handle.. Thanks for the report! I'll investigate why this is happening.

Edit 2: The trouble seems to be that the first call to 'readline' receives the 'enter' you hit to type in the command.

  arc> (readline (stdin))
  ""
  arc>
I think your examples might work if you put the code into a .arc file and try to run it like my original example..

-----

2 points by jsgrahamus 648 days ago | link

This probably seems weird, but I capture the data on a Windows system, then e-mail the data to a Linux system which is where arc resides. I assume Windows and Linux have different line endings.

Perhaps I need to check out the community version of arc?

Has anyone figured out a way to compile an arc routine? I saw an earlier thread on it, but no resolution.

Thanks for everything.

-----