Deconstruct string

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Deconstruct string

Post by Danton » Thu Aug 26, 2010 12:15 am

Hi All,

I'd like to deconstruct a string and get all the variables contained with in it.


I'm sure there's a "clever" way to do it.

The pattern is always the same; the variables are contained within quotes after the "=", separated by the &.

The name at the begining can change and is not that important the number of variables can also change.


eg CUST="26082"&"29185"&"35247"&"45407"&"65488"&"72471"&"110082"



Thanks
Danton

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Thu Aug 26, 2010 2:16 am

Try this:

Code: Select all

Let>String=CUST="26082"&"29185"&"35247"&"45407"&"65488"&"72471"&"110082"
Position>=",String,1,nPos,TRUE
Add>nPos,1
Length>String,nLength
MidStr>String,nPos,nLength,strSub
Separate>strSub,&,Items
This will return an array of variables, i.e.

ITEMS_1="26082"
ITEMS_2="29185"
ITEMS_3="35247"
ITEMS_4="45407"
ITEMS_5="65488"
ITEMS_6="72471"
ITEMS_7="110082"

And also return a variable with the count of the items, i.e.

ITEMS_COUNT=7

Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Post by Danton » Thu Aug 26, 2010 3:56 am

adroege wrote:Try this:

Code: Select all

Let>String=CUST="26082"&"29185"&"35247"&"45407"&"65488"&"72471"&"110082"
Position>=",String,1,nPos,TRUE
Add>nPos,1
Length>String,nLength
MidStr>String,nPos,nLength,strSub
Separate>strSub,&,Items
This will return an array of variables, i.e.

ITEMS_1="26082"
ITEMS_2="29185"
ITEMS_3="35247"
ITEMS_4="45407"
ITEMS_5="65488"
ITEMS_6="72471"
ITEMS_7="110082"

And also return a variable with the count of the items, i.e.

ITEMS_COUNT=7

:D Great, thanks, that works!!

Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Post by Danton » Thu Aug 26, 2010 5:04 am

adroege wrote:Try this:

Code: Select all

Let>String=CUST="26082"&"29185"&"35247"&"45407"&"65488"&"72471"&"110082"
Position>=",String,1,nPos,TRUE
Add>nPos,1
Length>String,nLength
MidStr>String,nPos,nLength,strSub
Separate>strSub,&,Items
This will return an array of variables, i.e.

ITEMS_1="26082"
ITEMS_2="29185"
ITEMS_3="35247"
ITEMS_4="45407"
ITEMS_5="65488"
ITEMS_6="72471"
ITEMS_7="110082"

And also return a variable with the count of the items, i.e.

ITEMS_COUNT=7

Thanks again,

I tried the next step myself but I've been pulling my hair out trying to get it to work.

Ideally I'd like to post this array to excel using DDEPoke


DDERequest>Excel,%filename%,R1C1,strData,10
Let>String=strData
Position>=",String,1,nPos,TRUE
Add>nPos,1
Length>String,nLength
MidStr>String,nPos,nLength,strSub
Separate>strSub,&,Items

Let>xlRow=%xlRow%+1
DDEPoke>Excel,%filename%,R%xlRow%C1,%ITEMS_1%
Let>xlRow=%xlRow%+1
DDEPoke>Excel,%filename%,R%xlRow%C1,%ITEMS_2%



I can get this to work, but it's clumsy. I'd like to use a loop and repeat until the end of the array. Basically can we have a count with the items_x variable?

Thanks again
Danton

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Aug 26, 2010 9:12 am

The count is in items_count. Separate produces items_1 ... items_n plus items_count

So you can loop through the array:

Code: Select all

Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_item=items_%k%
  ...
Until>k=items_count
And I would use XLGetCell and XLSetCell rather than DDERequest/DDEPoke
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Post by Danton » Thu Aug 26, 2010 10:25 pm

Thanks,

I'm learning :lol:
I got close in my attempt, but not close enough.

Thanks for the tip on xlGetcell too..


Danton

Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Post by Danton » Fri Aug 27, 2010 11:26 pm

mtettmar wrote:The count is in items_count. Separate produces items_1 ... items_n plus items_count

So you can loop through the array:

Code: Select all

Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_item=items_%k%
  ...
Until>k=items_count
And I would use XLGetCell and XLSetCell rather than DDERequest/DDEPoke

Strange. For some reason DDERequest has suddenly stopped working. It worked flawlessly on a previous script in the past and was working earlier today and now it's not working on either the new or old script I get a "strdata=DDE_service_invalid"

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts