What I can't understand is how to match what he is starting with which is
not  11;13;17

Linda

-----Original Message-----
From: programming-boun...@forums.jsoftware.com
[mailto:programming-boun...@forums.jsoftware.com] On Behalf Of linda
Sent: Saturday, February 15, 2014 5:13 AM
To: programm...@jsoftware.com
Subject: Re: [Jprogramming] awk-like J sentences?

I speak only J.  Once Raul translated awk I could understand what it was
doing.  Here is how I would think of the problem as a "native" speaker of J.

]A=:11;13;15
---T--T--┐
│11│13│15│
L--+--+---
   
   >A
11 13 15
   
   
   2{.>A
11 13
   
   +/2{.>A
24
   
   ]B=: ":+/2{.>A
24
   
   B;' WEEKS'
---T------┐
│24│ WEEKS│
L--+-------
   
   B,' WEEKS'
24 WEEKS
   
   f=: 13 :'":+/2{.>y'
   
   f A
24
   
   f
[: ": [: +/ 2 {. >

  (f A),' WEEKS'
24 WEEKS 

 Linda  
   
  
-----Original Message-----
From: programming-boun...@forums.jsoftware.com
[mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Raul Miller
Sent: Friday, February 14, 2014 11:40 PM
To: Programming forum
Subject: Re: [Jprogramming] awk-like J sentences?

One thing to be careful about - awk is designed for a unix command line.
When using J, you should assume a J command line. With a little work, you
can use a J program at the unix command line, but I am not really
comfortable with the way that works, yet.

Meanwhile, at the unix command line you can work with stdin, hereris
scripts, or with files. Similarly, with J, you can work with function
arguments (vaguely like stdin, but right to left, instead of left to
right), with scripts, and with files.

In all cases, the natural unit of computation, for this problem - just like
in awk - would be a single line. I've seen some other code here, but this
is pretty simple:

BEGIN {FS=";"}

becomes

';'&.
or
,&':'

Where in AWK you are giving the program a directive, here you are
physically incorporating the semicolon character in the line.

Also, when coding J, it's good to give your code some test data, so you can
verify that it's working the way you like. So, here's a first attempt:

   ": +/ 2 {. 0&".;._2 ,&';' '11;13;17'

24


I went with the ,&';' approach. if I had used ';'&, I would replace ;. _2
with :. _1 in that expression. (This is the modifier which chops up the
line in "fields".


I went with 0&". to convert string representations of numbers to character
representation. This mimics a feature which is implicit in awk.


I did not directly mention the field numbers in my example. I probably
should have. To fix that replace 2 {. with 0 1 { (J has 0 for the first
element where AWK uses 1). Finally, +/ inserts + between the two values and
": converts back to text. Quite likely converting back to text is an
unnecessary step, but that's what AWK is doing so I included it here. If
you want this as a named verb, you could go like this:


   addtwo=:verb def '": +/0 1 { 0&".;._1 '';''&, y'

   addtwo '11;13;17'

24


That's probably not completely intuitive, but I remember taking quite some
time before I was really comfortable working with awk. Everything takes
time to learn, and it's really easy to forget that after you know it well.


Once you have this, here's using it with an inline script (J's variation on
hereis documents):


   addtwo;._2(0 :0)

1;2;3;4;5

6;7;8;9

10;11;

)


Executing this gave me the result:

3

13

21


If you want to run against a file, replace (0 :0) with fread filename.


I hope this helps,


-- 

Raul









On Fri, Feb 14, 2014 at 9:51 PM, Lee Fallat <ircsurfe...@gmail.com> wrote:

> Hey there,
>
> As new user to J (but several years experience with C and Java), I
> find it very, very interesting. The power of its one liners and
> mathematical heritage really have me hooked.  I was wondering though
> if it has similar capabilities as awk. What's the equivalent to this
> awk script in J?:
>
> BEGIN { FS=";" }
> { print $1+$2 }
>
> This script sets a FieldSeparator to ;, and then for every "row", add
> the first and second column and prints it. I would like to replace awk
> with J!
>
> Thank you,
>
> Lee
>
> P.S. Excuse me if I've misidentified J sentences. (Sentences ->
> statements?)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to