Welcome, Guest. Please Login or Register
Tinderbox
  News:
Looking for tech support? Contact info@eastgate.com
  HomeHelpSearchLoginRegister  
 
Pages: 1
Send Topic Print
Global string changes (Read 2328 times)
james a. foster
Full Member
*
Offline



Posts: 99

Global string changes
Feb 15th, 2012, 11:27pm
 
How do I change commas to semicolons everywhere in a user field? I have a file $Authors, of set type. But it's entries use commas instead of semicolons. For example, it may be "AB Arthur, BC Zaphod, and DE Trillian" when it should be  "AB Arthur; BC Zaphod; DE Trillian"

I'm guessing there is something really obvious that I'm missing.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4469
Southsea, UK
Re: Global string changes
Reply #1 - Feb 16th, 2012, 4:36am
 
Actually a correct set wouldn't be "AB Arthur; BC Zaphod; DE Trillian" - there would be no spaces between terms. it would be "AB Arthur;BC Zaphod;DE Trillian". A semi-colon isn't needed after the last item but won't upset TB if present, e.g. as a result of some code-based list production where all items have the same terminator applied.

Currently, you've data like "AB Arthur, BC Zaphod, and DE Trillian" which if added to a Set would create a single list value. Bear in mind though that just changing the source from commas to semi-colons isn't enough though as "and DE Trillian" isn't an author. Therefore, I guess you'll need to write some queries to strip terms like 'and', 'et al' etc. at some point in your import or - depending on the workflow/volume of data, correct such terms manually.

OK, as Set attribute values we've comma+space delimited strings instead of such strings' contents being discrete TB list items. To resolve that, this action code works (testing in v5.10.1):

$MySetA = $MySet.replace(", ",";");

Alternatively, if an 'and ' is the only non-author content apart from the commas we can use:

$MySetA = $MySet.replace("and ","").replace(", ",";");

Note how the dot-operators let us 'chain' two discrete actions together - and the 'and' sub-strings are replaced with nothing then all the ', ' sub-strings are replaced with semi-colons. If the were more garbage terms than just 'and ' I'd try and weed them before the point and certainly not try and chain many .replace() arguments as in other than a very small test something is likely to get overloaded. IOW, I don't think the design intent for chaining would be for twenty chained .replace() calls. YMMV depending on your TBX's size/complexity/content.

If a note's $MySet had 2 discrete values "ant, bee, cow;dog, eel, fox", $MySetA now holds 6 discrete values. Actually you can set the data back on itself, with either of:

$MySet = $MySet.replace(", ",";");
$MySet = $MySet.replace("and ","").replace(", ",";");

... but if testing code and working on real data it might be worth cleaning the data into a new attribute, checking it in the latter and only then overwriting it back into the correct attribute (and deleting the temporary one).

The above tests were done in a $Rule but a more real-world scenario is you'll make and agent to find the data to clean:

Query:  $Authors      (only match notes with data in $Authors)
Action:   $Authors2 = $Authors.replace(", ",";");

Check that Set attribute $Authors2 is giving you the results expected and then change the agent's action:

Action:   $Authors = $Authors2;

You can now delete $Authors2. Of course, this whole action assumes the process is once-only.  I guess you could use either of:

$Authors = $Authors.replace(", ",";")
$Authors = $Authors.replace("and ","").replace(", ",";")

...and leave it running all the time to clean new data being added in non-TB-list form. I'd be more tempted to import author data to some other attribute and only then clean it into $Authors so as to have more control.

Without seeing more of the workflow and how dirty the source data is, it's hard to conjecture much further - though I would suggest looking at the export methods of any upstream app to see if it can export the author info in TB list-friendly format.

P.S. I'm aware the usage of .replace() above doesn't match documented usage. I'm not sure why, but it seems some previous limitations of conts/replace against sets/lists has been removed since these features were added.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4469
Southsea, UK
Re: Global string changes
Reply #2 - Feb 16th, 2012, 12:54pm
 
Further testing showed my notes on List/Set.replace() were wrong - the issue is narrower than I'd thought and to do with the ^ and $ regex operators matching as the user might expect.  The aTbRef page is now corrected (and now draws on what I learned from this morning's question here!).

A further benison from that better understanding of List/Set.replace() is that it shows the way for a workaround for List/set.contains()'s limitations searching for sub-strings of list values. That latter page is also updated with the workaround.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
james a. foster
Full Member
*
Offline



Posts: 99

Re: Global string changes
Reply #3 - Feb 16th, 2012, 6:00pm
 
Perfect! I just hadn't noticed the .replace() operator.

Thanks!
Back to top
 
 
  IP Logged
Pages: 1
Send Topic Print