The approach I took to get around potential issues with underscores was to
use a verb that does the same as deb (delete extraneous blanks) but for
underscores. That is it removes leading and trailing underscores, and
restricts consecutive underscores to 1.

   require 'tables/csv'
   coinsert 'pdsv'
   >uniqify coerce2Name&.> '_foo##bar_';'foo(bar)';'_ 7foo bar'
foo_bar0
foo_bar1
X7foo_bar




On Mon, May 26, 2014 at 9:06 PM, Jan-Pieter Jacobs <
[email protected]> wrote:

> Just a little note: Replacing stuff with underscores should be used with
> care, as the resulting variable names might collide with stuff in locales.
>
> eg: a name as 'foo##bar' would end up being foo__bar, and assigning
> something to this variable ends up with an value error on bar. If bar
> existed before, you can get all sorts of errors, but you won't end up with
> a variable named 'foo__bar' for sure.
>
> Just to say, we'd better take something that has no special meaning instead
> of _, which is sort of hard to find in J... x's all the way maybe?
>
> Jan-Pieter
>
>
> 2014-05-26 10:39 GMT+02:00 'Pascal Jasmin' via Programming <
> [email protected]>:
>
> > 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

Reply via email to