(Apologies if e-mail eats my leading spaces - Henry - how do you do it?) samp=. '1 2;3 4;99 1' FS=. ';' <;._1 FS,samp +---+---+----+ |1 2|3 4|99 1| +---+---+----+ ".&><;._1 FS,samp 1 2 3 4 99 1 +/"1".&><;._1 FS,samp 3 7 100
On Fri, Feb 14, 2014 at 10:22 PM, Henry Rich <henryhr...@nc.rr.com> wrote: > If I had awk, I would use it to solve this problem. Assume I don't. > > Here is a test file: > file =: 0 : 0 > 1 ; 3 > 4 ; 5 > ) > > In J, I would write: > > addcols12 =: [: +/"1 [: ".@> 2 {."1 ';'&(<;._1@,);._2 > > testing: > > addcols12 file > 4 9 > > If I were a (very precocious!) beginner, I would write > > NB. y is a file with LF termination for lines, and ';' as > NB. field separator > NB. Add first 2 fields of each line > addcols12 =: 3 : 0 > NB. split on lines > lines =. <;._2 y > NB. split on ; > linedelim =. ';' <;._1@,&.> lines > NB. Take first two fields of each line > linefields12 =. 2 {.&> linedelim > NB. Convert to numeric and add em up > +/"1 ".@> linefields12 > ) > > testing: > > addcols12 file > 4 9 > > > > On 2/14/2014 9:51 PM, Lee Fallat 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 > -- Devon McCormick, CFA ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm