Subscribe Bookmark RSS Feed

How to use 'include' to write compact code?

poulravn

Community Trekker

Joined:

Jan 25, 2012

Hi,

I repeatedly open a certain file type, which needs special import settings to opening correctly.

I would like to be able to write these import settings in a short compact form. For instance:

dt=open("fileref",import settings("string_that_parses_correctly");

where "string_that_parses_correctly" is the whole string, that holds the settings' conditions. In my case I use:

"string_that_parses_correctly"=

"End Of Line( CRLF, CR, LF ),

End Of Field( Tab ),

Strip Quotes( 1 ),

Use Apostrophe as Quotation Mark( 0 ), 

Scan Whole File( 1 ),

Treat empty columns as numeric( 0 ), 

Labels( 1 ),

Column Names Start( 1 ),

Data Starts( 2 ),

Lines To Read( All ),

Year Rule( "20xx")"

I captured these settings from the script generated by opening one of these files.

So is there a way to define a string, for instance as a global variable, that always is set, so that I can use it when opening the file as shown.

I hope it makes sense.

Regards

Poul Ravn Sørensen

4 REPLIES
poulravn

Community Trekker

Joined:

Jan 25, 2012

HI, I am replying to this myself.

My problems boils down the issue of combining strings into a parseable piece of jsl.

I have defined a string that contains the string, that set the special settings for the xls (csv) import. It start out: Import Settings(. . . );

Then I want dynamically to assign the file, that should be opened, like:

xls_fileid = "\\server\folder\filename";

Finally I want to combine these two like this:

openxls=xls_fileid||xls_settings;

eval(parse(openxls));


But somehow get the double quotation marks mixed up.

Anyone has an example of how this is done?

Regards

Poul

pmroz

Super User

Joined:

Jun 23, 2011

You can either use \[ ... ]\ to put double quotes into a string.

Or escape them with \!".  Here are some examples.

print("Hello World");

// The following statements yield the same results:

print("\!"Hello World\!"");

print("\["Hello World"]\");

From the log:

"Hello World"

"\!"Hello World\!""

"\!"Hello World\!""

poulravn

Community Trekker

Joined:

Jan 25, 2012

Hi again, here is my JMPstart.jsl code, that sets the xls_settings (why I sometimes can't paste text into this window I do not know), :

xls_settings_pic.jpg

Here are my attempts to biuld the string the opens the actual Excel file:

xls_fileid=\\servername\foldername\filename.xls;

dlm=",";

dqm="\["]\";

openxls=xls_fileid||dqm||dlm||xls_settings;

and then:

eval(parse(openxls));

But the quotes keep causing errors.

Regards Poul

pmroz

Super User

Joined:

Jun 23, 2011

You're missing double quotes around the file reference.

Your command openxls doesn't do anything.  You need to add OPEN to it.

One of the nice things about JSL is its interactivity.  As you build the string, highlight it and click Run.  Check the log window and see what you've created.  Keep iterating until you get what you want.

This is possibly what you're trying to do:

xls_fileid="\\servername\foldername\filename.xls";

xls_settings = "\[

Import Settings(

     End of line (crlf, cr, lf),

     end of field(tab),

     strip quotes(1),

     use apostrophe as quotation mark(0),

     scan whole file(1),

     treat empty columns as numeric(0),

     labels(1),

     column names start(1),

     data starts(2),

     lines to read(all),

     year rule("20xx")

)]\";

openxls = evalinsert("\[open("^xls_fileid^", ^xls_settings^) ]\")

eval(parse(openxls));