Many thanks for all the help!
on 2019-12-23 12:04:59 phspaelti wrote:
Just to answer your immediate question, it isn't the the .split command that is putting quotes around things, it's the Type text command that's doing that. Or to put it more accurately, the Type text creates a string context; your variable $TheFields is an array, so in the string context it will output the string representation of the array, which is quotes around the values and separate the values with comma. If you wanted to output the array with comma separated values and no quotes you can use the join command.
CODE: SELECT ALL
Type text $TheFields.join(',')
But if you're doing this, then I wonder why you are splitting them in the first place.
You could, of course, join the values back together with other separators, e.g., a space, a return, a tab, etc. Alternatively you might process the values of the array one at a time in some way. Not sure what you are trying to achieve here.
To explain why I was writing code that is, frankly, silly (reading a set of fields then writing them again)... I don't intend to do that.
This is the initial stage of what is going to be a very long macro. (It probably should have been done in Perl, but we decided not to, for complicated reasons.) When it is done, it will takes the data from one CSV file and arrange it into another CSV file with different fields. If it really matters, the input data is a "household" -- one or more people all listed in one record. We have to take all the people in each household and create a record for each. What starts as one record in the input file might be as many as six in the output file!
So what I'm really trying to do is get the data from the first CSV file into an array, so that I can test various fields and write the proper output CSV. I'm still on the first part of that, trying to get the data into an array. So your answer is very helpful; the .split was working, and I was misinterpreting it.
On 2019-12-23 09:54:23, phspaelti wrote:
And one more point: You mention "valid CSV". If the stuff you are trying to split into fields is CSV you might note that the .split command will not give you the correct result in the general case, since quoted fields will be used to protect commas within fields. But I'm sure you know that already.
That problem I'd already taken care of; the commas inside quotes have been changed to another character. So the only commas in the input string are those that separate the fields in the CSV. Regular expressions I can do; my problem is figuring out the NWP macro language without the a complete reference for people who learned the wrong sort of programming.
If you write the find expression cleverly you can also get rid of the protective quotes at the same time.
I'm working on it.
Some other parts of my code are already better than they would have been because of your help. Some of your other suggestions here use tricks I hadn't learned yet -- e.g. using join instead of type -- but you have given me much to work with. Again, thanks!