I gave the program for cutting on LF because it covers the more general case. I used _1 instead of _2 because it is easier to do LF , text than LF ,~ text... I assumed that the file did not end with LF but contained LF to separate each row of the array or was not present in the case of a single row array.
1!:1 and 1!:2 are faster than fread, fwrite if you know what the format of the file is? On Thu, Feb 12, 2009 at 1:58 PM, Devon McCormick <[email protected]> wrote: > +/ ( [: ".;._1 ','&,);._1 LF , '1,2,3,4,5' > 1 2 3 4 5 > NB. because > $( [: ".;._1 ','&,);._1 LF , '1,2,3,4,5' > 1 5 > NB. You probably want something like: > +/, ([:".;._1 ','&,);._1 LF , '1,2,3,4,5' > 15 > > If the numbers had still been character, you would have gotten an error as > "+" is not defined for characters. > > The real question is why you are cutting on LF when there aren't any in > your > string: are you planning to use this expression on strings with embedded > LFs? Like this: > > +/ ( [: ".;._1 ','&,);._1 LF , '1,2,3,4,5',LF,'10 20 30 40 50' > 11 22 33 44 55 > > When reading characters from file to convert them to numbers, I find it > handy to use cut with a "_2" qualifier to cut on the last element, e.g. > > ('1,2,3,4,5',LF,'10 20 30 40 50',LF) fwrite 'file.txt' > 25 > <;._2 fread 'file.txt' > +---------+--------------+ > |1,2,3,4,5|10 20 30 40 50| > +---------+--------------+ > NB. leading to > ([:+/".) &> <;._2 fread 'file.txt' > 15 150 > > > On Thu, Feb 12, 2009 at 8:37 AM, Steven Taylor <[email protected]> wrote: > > > +/ 1 2 3 4 5 > > 15 > > > > +/ ( [: ".;._1 ','&,);._1 LF , '1,2,3,4,5' > > 1 2 3 4 5 > > > > Are the numbers still in string format? > > > > Reason: J familiarisation and would like to run some tests. > > > > thanks > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > > > > -- > Devon McCormick, CFA > ^me^ at acm. > org is my > preferred e-mail > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
