Yes my description wasn't very clear.
The use case that I'm most interested in is where you read in a delimited
file with a header and want to manipulate or summarise it. For me this is
often something like:

csvfile=: 0 :0
Id,YOB,Sex,ProtBV,FatBV,MilkBV,BW,Rel
443,2007,M,2.83,1.21,4.65,20.88,0.11

458,2013,F,2.30,1.59,4.99,20.42,0.04

440,2002,M,2.56,1.09,4.92,20.95,0.51

422,2000,M,2.91,1.36,4.74,20.86,0.70

424,2009,F,2.81,1.44,4.93,20.16,0.74

)

NB. Then I will interactively work with the data to answer the question at
hand.
   assign2hdr fixcsv csvfile
   Id #~ YOB > 2008    NB. ids of animals born after 2008

458 424

   Sex +//. BW             NB. breeding worth by sex

62.69 40.58

   +/ FatBV #~ (YOB > 2008) *. Sex=<,'F'   NB. fat breeding value for
females born after 2008

3.03


This is much more logical if the field names aren't preceded by a prefix of
some type.
Sometimes there are one or 2 field names that don't follow J naming
conventions. It is easier to force them to a legal J name than to provide a
whole new set of names (or prefix all names).

Of course if the current field names are bad enough (or there aren't any)
then you can provide your own set of names:
   hdr=: ;:'id yob sex fatbv protbv milkbv bw rel'
   hdr assign2hdr }. fixcsv csvfile

If you want to prefix them with something meaningful to distinguish them
from a second set then you can adjusting the header names first.
   addPrefix=: (boxopen@[ ,&.> ])
( 'jer_' addPrefix hdr) assign2hdr }. fixcsv csvfile

However the most common use case is simplest to use.


On Tue, May 27, 2014 at 4:56 AM, Raul Miller <[email protected]> wrote:

> Hypothetically speaking, if you want your code to be readable, and if you
> wish to also be entering the names literally (as opposed to being data
> driven), I imagine that you should not be using names which need to be
> modified before they can be used. And in that case you shouldn't need a
> cover verb for the assignment?
>
> Or have I misunderstood what you are trying to do?
>
> Thanks,
>
> --
> Raul
>
>
> On Mon, May 26, 2014 at 7:08 AM, Ric Sherlock <[email protected]> wrote:
>
> > Here is my solution based on the discussion. I don't think that I'd want
> to
> > prefix the header names normally. For me a lot of the benefit of
> assigning
> > the columns to the header is that my code becomes more readable. This get
> > spoilt a bit when all the names have prefixes. However assign2hdr takes
> an
> > optional left arg of the header names, so if you wanted to add a prefix
> > then that is possible at that point.
> > The solution below doesn't yet handle the problem with header names
> > colliding with local names that spawned this thread.
> >
> > forceLeadingAlpha=: ,~ 'X' {.~ [: -.@e.&Alpha_j_ {.
> > replaceNonAlpha=: (-.@e.&AlphaNum_j_)`(,:&'_')}
> > deus=: #~ (+. (1: |. (> </\)))@('_'&~:)    NB. delete extraneous
> > underscores
> > coerce2Name=: deus@replaceNonAlpha@forceLeadingAlpha@deb
> >
> > assign2hdr=: 3 : 0
> >
> >   'hdr dat'=. split y
> >
> >    hdr assign2hdr dat
> >
> > :
> >
> >    hdr=. uniqify_pdsv_ coerce2Name_pdsv_&.> x
> >
> >    dat=. |: makenumcol y
> >
> >    idx=. I. 2 ~: (3!:0)&> {."1 dat
> >
> >    ((<<<idx){hdr)=: <"1 (<<<idx) { dat
> >
> >    (idx{hdr)=: idx { dat
> >
> >   EMPTY
> >
> > )
> >
> >
> > On Mon, May 26, 2014 at 8:55 PM, Raul Miller <[email protected]>
> > wrote:
> >
> > > That sounds like a good idea.
> > >
> > > Except I think we need three parameters:
> > > list of names
> > > disambiguating prefix
> > > list of values
> > >
> > > I think I'd like the list of names be x, the prefix be adverb argument
> > and
> > > the list of values be y.
> > >
> > > So here's an implementation:
> > > masgn=:1 :0
> > > :
> > >   bad=.a.-.;'0Aa' ([ (+ i.) 1 + -~)&.(a.&i.)&.> '9Zz'
> > >   names=. ('_' I.@e.&bad@]} m&,)&.>x
> > >   (names [erase 'names bad x y')=: y
> > > )
> > >
> > > Example use:
> > >
> > >    (;:'a 1.1 b') 'z' masgn i.3 3
> > > 0 1 2
> > > 3 4 5
> > > 6 7 8
> > >    za
> > > 0 1 2
> > >    z1_1
> > > 3 4 5
> > >    zb
> > > 6 7 8
> > >
> > > Of course I probably went a little overboard here with the
> > implementation.
> > > So feel free to strip out parts you don't need. And maybe add a 0 0$ at
> > the
> > > beginning of the result line, if you do not like results.
> > >
> > > Thanks,
> > >
> > > --
> > > Raul
> > >
> > >
> > >
> > > On Mon, May 26, 2014 at 4:39 AM, 'Pascal Jasmin' via Programming <
> > > [email protected]> wrote:
> > >
> > > > The way I would do it would be to add a prefix passed from x
> parameter,
> > > > and replace spaces (or illegal characters ') with underscores.
> >  Assuming
> > > > other illegal characters are rare should be fine, but the advantage
> of
> > > > prefixing all columns helps both in preventing collisions with
> general
> > J
> > > > names, and allows processing multiple such files without collision.
> > > >
> > > >
> > > > ----- Original Message -----
> > > > From: Ric Sherlock <[email protected]>
> > > > To: Programming JForum <[email protected]>
> > > > Cc:
> > > > Sent: Sunday, May 25, 2014 4:20:16 PM
> > > > Subject: Re: [Jprogramming] Multiple assignments
> > > >
> > > > I recently released an update to addons/dsv that contains a new
> > utitlity:
> > > > assign2hdr
> > > >
> > > > testcsv=: noun define
> > > > # The following is an example of a comma-separated file with
> > > > # comment lines at the start of the file indicated by the fact they
> > > >
> > > > # start with the # symbol.
> > > >
> > > > id,bar code,name
> > > >
> > > > 24582621,119533,DELTOP DAVINCI
> > > >
> > > > 25422991,155439,AMBZED ROSCOE S2F
> > > >
> > > > 25784612,135624,TEF SHADOW BLARIS
> > > >
> > > > 22063188,102545,BIG P BLONDEL PRIM
> > > >
> > > > 20803506,137609,MONGA FLOL
> > > >
> > > > 27360900,107865,FRAMBIN R NOGN ET
> > > >
> > > > )
> > > >
> > > >
> > > >    fixcsv testcsv
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |id |bar code|name |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |24582621|119533 |DELTOP DAVINCI |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |25422991|155439 |AMBZED ROSCOE S2F |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |25784612|135624 |TEF SHADOW BLARIS |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |22063188|102545 |BIG P BLONDEL PRIM|
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |20803506|137609 |MONGA FLOL |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > > |27360900|107865 |FRAMBIN R NOGN ET |
> > > >
> > > > +--------+--------+------------------+
> > > >
> > > >
> > > >    assign2hdr fixcsv testcsv
> > > >
> > > >    bar_code
> > > >
> > > > 119533 155439 135624 102545 137609 107865
> > > >
> > > >
> > > > It does try to automatically coerce names, but does so by removing
> > > leading
> > > > blanks and any leading chars that aren't valid starting chars for a
> > name.
> > > > Adding as 'X' in front may be better than that.
> > > >
> > > > On May 26, 2014 5:04 AM, "Devon McCormick" <[email protected]>
> wrote:
> > > >
> > > > > Assigning column names this way can be very useful.  However, it
> > > requires
> > > > > that column names be valid J variable names.  The R programming
> > > language
> > > > > has a feature like this that includes automatic coercion of invalid
> > > names
> > > > > for at least a couple of simple cases - something like changing
> > > embedded
> > > > > spaces to underscores and prefixing any name beginning with a
> numeral
> > > > with
> > > > > "X".
> > > > >
> > > > > It might be useful to publicize a simple set of conversions like
> this
> > > > for J
> > > > > names on the wiki so that there's at least a stab at
> standardization.
> > > > >
> > > > >
> > > > > On Sun, May 25, 2014 at 12:58 PM, J. Patrick Harrington
> > > > > <[email protected]>wrote:
> > > > >
> > > > > > I've always liked this economy of assignment:
> > > > > > 'e f g'=. s=. >1 2;3 4;5 6
> > > > > >   f
> > > > > > 3 4
> > > > > >   e
> > > > > > 1 2
> > > > > > -- but I wondered if it were possible to extend this where the
> left
> > > > hand
> > > > > > side is not explicit:
> > > > > >    abc=. 'a';'bb';'ccc'
> > > > > > ┌─┬──┬───┐
> > > > > > │a│bb│ccc│
> > > > > > └─┴──┴───┘
> > > > > > I found I could make a single assignment:
> > > > > >    (>1{abc)=. 1{s
> > > > > >    bb
> > > > > > 3 4
> > > > > >   a
> > > > > > |value error: a
> > > > > > But I didn't want to put this into a loop.
> > > > > > It was neat to find that I could write:
> > > > > >    (abc)=. s
> > > > > >    a
> > > > > > 1 2
> > > > > >    bb
> > > > > > 3 4
> > > > > >   ccc
> > > > > > 4 5
> > > > > >   abc
> > > > > > ┌─┬──┬───┐
> > > > > > │a│bb│ccc│
> > > > > > └─┴──┴───┘
> > > > > >   This is useful in working with data files output
> > > > > > by a stellar evolution program where I have ASCII tables with 90
> > > > columns
> > > > > > and over 1000 rows, and each
> > > > > > column has a name 1 to 25 characters long: I wanted
> > > > > > to read this into a J session, assigning the column
> > > > > > values to each of the respective names. Turns out it
> > > > > > can be done with one line of the form
> > > > > >    (head)=: data_array
> > > > > > where
> > > > > >    head
> > > > > > ┌────┬────┬──────┬────┬────┬──────────┬────────┬────────────
> > > > > > ─────────────┬───────────────────┬─
> > > > > > │zone│logT│logRho│logP│logR│luminosity│eps_grav│log_abs_
> > > > > > eps_grav_dm_div_L│signed_log_eps_grav│..
> > > > > > └────┴────┴──────┴────┴────┴──────────┴────────┴────────────
> > > > > > ─────────────┴───────────────────┴─
> > > > > > (see http://www.astro.umd.edu/~jph/mesa_read.ijs) Another
> example
> > of
> > > > the
> > > > > > "black hole of J".
> > > > > >
> > > > > > I don't know where in the documentation this can be
> > > > > > found - I just got there by experimentation.
> > > > > >
> > > > > >                                        Patrick
> > > > > >
> > > > > > P.S. This started as a post asking for
> > > > > >      help in getting rid of a loop, but
> > > > > >      I found the solution along the way.
> > > > > >
> > > ----------------------------------------------------------------------
> > > > > > 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
> > > >
> ----------------------------------------------------------------------
> > > > 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
> >
> ----------------------------------------------------------------------
> 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