| View previous topic :: View next topic |
| Author |
Message |
seott Newbie
Joined: 11 Mar 2009 Posts: 8
Reputation: 100   Earn Points, Win a T-Shirt
|
Posted: Wed Mar 11, 2009 6:45 pm Post subject: Array From A ReadLn |
|
|
Hi everyone, I love this program...
How do I create an array from a comma separated line in a file?
I'd like to run a ReadLn on say... line 2 of a file that would ready
Red,Green,Blue,Yellow
And I want to be able to say
let>variable= array[4]
Which would equal yellow. Anyone done this?
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
seott Newbie
Joined: 11 Mar 2009 Posts: 8
Reputation: 100   Earn Points, Win a T-Shirt
|
Posted: Wed Mar 11, 2009 7:24 pm Post subject: Figured it out |
|
|
Ah, figured it out, needed to use separate.
Another question though...
If I'm using separate to go through and using a comma as a delimiter - I have a currency in one of the fields that has double quotes around it...
so like
red,green,blue,"555,000,000"
How would I get it to treat the currency as one unit instead of three? Is there a way I can instruct it to leave items in double quotes alone?
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
JRL Automation Wizard
Joined: 10 Jan 2005 Posts: 1804 Location: Iowa Reputation: 525.9   votes: 45 Earn Points, Win a T-Shirt
|
Posted: Wed Mar 11, 2009 10:31 pm Post subject: |
|
|
Off the top of my head I don't have a generic solution for you. But, assuming that the format you've presented is fixed, you might do 2 separates> The first one would use ," (comma quote) as the delimiter and would divide the line in to two variables. The first variable value would be red,green,blue and could then be separated using a comma as the delimiter.
The second variable from the first Separate> would be the currency value plus one quote. Run StringReplace> on the variable to remove the quote.
Hope this is helpful.
The following is untested:
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
jpuziano Automation Wizard

Joined: 30 Oct 2004 Posts: 772
Reputation: 80.6   votes: 8 Earn Points, Win a T-Shirt
|
Posted: Wed Mar 11, 2009 10:36 pm Post subject: Re: Figured it out |
|
|
| seott wrote: | so like
red,green,blue,"555,000,000"
How would I get it to treat the currency as one unit instead of three? Is there a way I can instruct it to leave items in double quotes alone? |
Well... if your data was the following...
red;green;blue;"555,000,000"
...you could just use ; as your delimiter in the Separate> command and it would do what you want... yes?
So, just convert your data to this new format... then use Separate> on it.
Here's some pseudo code to do that:
- process each line separately
- for a flag, create a variable like Let>inside_double_quotes=0
- inspect each character of a line
- if the char is " then toggle inside_double_quotes (if its 1 make it 0, if its 0 make it 1)
- if the char is , then if inside_double_quotes=0 then replace , with ; otherwise leave it unchanged
- if the char is anything else, leave it unchanged
Let us know how you do...
P.S. If your original data could possibly contain a ; character... use some other obscure character like vertical bar | etc. _________________ jpuziano
Note: When posting code to the forum... please:
- highlight the code then click the "Code" button
- check the "Disable HTML in this post" checkbox
- check the "Disable Smilies in this post" checkbox
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
jpuziano Automation Wizard

Joined: 30 Oct 2004 Posts: 772
Reputation: 80.6   votes: 8 Earn Points, Win a T-Shirt
|
Posted: Wed Mar 11, 2009 11:04 pm Post subject: |
|
|
Hi seott,
This is quick and dirty... but it works:
You could extend this to process more lines, etc.
I would also reset inside_double_quotes to 0 each time you started processing a new line... in case your data ever contained an odd number of double quote " characters.
Take care _________________ jpuziano
Note: When posting code to the forum... please:
- highlight the code then click the "Code" button
- check the "Disable HTML in this post" checkbox
- check the "Disable Smilies in this post" checkbox
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
Bob Hansen Automation Wizard

Joined: 24 Sep 2002 Posts: 2414 Location: Salem, New Hampshire, US Reputation: 300   votes: 17 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 2:16 am Post subject: |
|
|
Three steps:
1. Use a RegEx to replace commas inside double quotes with a tilde.
2. Use Separate as normal
3. Use StringReplace to replace tildes with commas on each separated value _________________ Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
jpuziano Automation Wizard

Joined: 30 Oct 2004 Posts: 772
Reputation: 80.6   votes: 8 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 3:18 am Post subject: |
|
|
| Bob Hansen wrote: | Three steps:
1. Use a RegEx to replace commas inside double quotes with a tilde.
|
Hi Bob,
I see, you're modifying the commas inside the double quoted numbers... instead of the others.
Sounds good... but what if the data was:
Let>line=red,green,blue,"555,000,000",yellow,"22,000"
How would you code that regex pattern so it would target and replace the three commas inside the above numbers... without having it target the commas on either side of yellow?
Anyone? _________________ jpuziano
Note: When posting code to the forum... please:
- highlight the code then click the "Code" button
- check the "Disable HTML in this post" checkbox
- check the "Disable Smilies in this post" checkbox
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
jpuziano Automation Wizard

Joined: 30 Oct 2004 Posts: 772
Reputation: 80.6   votes: 8 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 9:02 am Post subject: |
|
|
Hi Bob,
Here's what I came up with...
From the above, it follows that if you wanted to be able to handle numbers containing one comma, two commas, three commas, i.e. of the form:
n,n
n,n,n
n,n,n,n
...where n = any number of digits... then you'd need a separate RegEx command to handle each pattern.
Unless Bob, you know a pattern that can handle numbers containing any number of commas... and replace them with tilde... in just one RegEx call. If you do or anyone does, please share.
I like your approach though, far less code... and its fine if the numbers are all in the same range, say might contain one or two commas tops.
However the RegEx approach would fail if the data contained a number with more commas in it than you supplied patterns for... while my code should work on numbers of any size... try the following:
_________________ jpuziano
Note: When posting code to the forum... please:
- highlight the code then click the "Code" button
- check the "Disable HTML in this post" checkbox
- check the "Disable Smilies in this post" checkbox
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
mtettmar Site Admin

Joined: 19 Sep 2002 Posts: 4217 Location: Dorset, UK Reputation: 621   votes: 28 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 10:42 am Post subject: |
|
|
Why not use DBQuery to read a CSV file:
That takes care of everything. Handles commas inside quotes etc.
Set Data Source to the path of the folder containing the CSV file. In DBQuery set the select statement to point to the CSV file in that folder. Set HDR to Yes if the first line of the CSV file is a header line and you don't want to return those values.
The following code loops through each field in each record:
I'm tempted to try and find a RegEx solution as that seems a nice challenge. But it's probably rather pointless when DBQuery does the job for you in so few lines of code. _________________ Regards,
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Please do not email/PM me for private support - post to the forum so that everyone benefits. For private support please send email via our web site.
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
mtettmar Site Admin

Joined: 19 Sep 2002 Posts: 4217 Location: Dorset, UK Reputation: 621   votes: 28 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 12:14 pm Post subject: |
|
|
Here's the RegEx solution:
You'll note that this returns:
sally
"1,2500"
fred
So we just need another RegEx to remove any starting and ending quotes from each field. So our loop would be:
So full code to create an array from a line of CSV and loop through each field:
_________________ Regards,
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Please do not email/PM me for private support - post to the forum so that everyone benefits. For private support please send email via our web site.
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
seott Newbie
Joined: 11 Mar 2009 Posts: 8
Reputation: 100   Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 12:17 pm Post subject: Thanks Everyone! |
|
|
Wow! Thanks everyone! I'm glad to see everyone is so helpful on this forum. Those were a lot of great solutions. I'm going to brainstorm which is the best for this particular situation or might use a mixture of them but thank you it's been a big help and I know I can get there now.
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
seott Newbie
Joined: 11 Mar 2009 Posts: 8
Reputation: 100   Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 12:26 pm Post subject: Great Blog |
|
|
Marcus, BTW, Great Blog. I feel famous now
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
seott Newbie
Joined: 11 Mar 2009 Posts: 8
Reputation: 100   Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 12:46 pm Post subject: MS 10 |
|
|
Are any of these commands exclusive to version 11? I have version 10
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
mtettmar Site Admin

Joined: 19 Sep 2002 Posts: 4217 Location: Dorset, UK Reputation: 621   votes: 28 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 1:32 pm Post subject: |
|
|
RegEx was added in v11.
DBQuery is supported by v10 but the ability to return field names in DBQuery was added in v11.
So the first DBQuery example above will work in v10. _________________ Regards,
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Please do not email/PM me for private support - post to the forum so that everyone benefits. For private support please send email via our web site.
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
jpuziano Automation Wizard

Joined: 30 Oct 2004 Posts: 772
Reputation: 80.6   votes: 8 Earn Points, Win a T-Shirt
|
Posted: Thu Mar 12, 2009 4:53 pm Post subject: |
|
|
Thanks Marcus, great examples and blog post! _________________ jpuziano
Note: When posting code to the forum... please:
- highlight the code then click the "Code" button
- check the "Disable HTML in this post" checkbox
- check the "Disable Smilies in this post" checkbox
Did this message help you? If so please reward the poster with Reputation Points? Reward Points
|
|
| Back to top |
|
 |
|