How to merge two attributes?

One more question: I am finding some of my attributes are similar and could be merged but it is my understanding there is no merging of attributes available.

Could Agents be used for this purpose? if so, how would you suggest fixing my older larger documents. (now I am keeping a controlled vocabulary which is helping)

Thanks in advance
Tom

Indeed, there isn’t a ‘merge’ operator. But this is toolbox. We don’t have a 'putUpShelf() function but we do have a drill, and screwdriver and a spirit level, etc. So with the latter in mind…

The general pattern here, is to merge attribute MyY with attribute MyX values and then delete MyY. So we make

  • an agent to find all notes with a value for MyY, i.e. query is: $MyY
  • code to merge MyY into MyX. Take care if either MyX or MyY is intrinsic as you’ll need to merge the originals’ values and not the aliases.
  • once done and checked, delete the agent and then delete user attribute Y. If you do use attribute descriptions, consider updating MyX’s description to include any new MyY-related aspects now stored in MyX.

How about the merge?

Merging two String-types:

$MyXString = $MyXString + $MyYString;

You might want to add a join string, e.g. " :: ":

$MyStringX = $MyStringX + " :: " + $MyStringY;

The top might merge “Green” and “Ham” to “GreenHam” whereas the second would give “Green :: Ham”. You could also put the Y value first:

$MyStringX = $MyStringY + " :: " + $MyStringX;

giving “Ham :: Green”.

Merging into a Set-type:

$MySetX = $MySetX + MySetY;

As the recipient is a Set-type duplicates are removed for you.

Merging into a List-type:

$MyListX = $MyListX + MySetY + $MyListZ;
$MyListX = $MyListX.unique;

If necessary you could sort the MyListX list as well.

Adding a String type to a Set or list:

$MyListX = $MyListX +$MyStringY;
$MyListX = $MyListX.unique;

or

$MySetX = $MySetX +$MyStringY;

…and so on.

Does that help. Don’t forget to delete the merged-and-not-needed attribute_but_ only after you’ve checked the merged data looks right!

Yes, thank you Mark.

Just to be clear, I assume (and after checking with aTbRef) that intrinsic attributes you are referring to above to be careful is because intrinsic attributes are not inherited, therefore you will need to include alias’s as well in these special cases.

Fortunately, in my case, 99.9% are either User Attributes, $Tags, $URL.

Here is the listing of those intrinsic variables I noted.

As always, many thanks
Tom

No. Intrinsic attributes do inherit. Their different nature is that an alias and the original of the same note can have different values for the same attribute.

Easiest way to understand. Make a new doc, add a note to the map view. Make an alias of the note. Now look at $Xpos for both original and alias. They are the same attribute with different values. This has to be so as the original and alias are in different map locations and $Ypos/$Ypos hold the {X,Y} map co-ords at which the top left corner of any map is drawn.

Note: user attributes cannot be made to be intrinsic.

ok. Thanks for the clarification.
I promise, one more question about merging and duplicates.

What about removing duplicates in non-set attributes? For example: if you have a string attribute, can you just convert it into a set attribute and get rid of the dups?
Any good strategies here when merging?

Thanks
Tom

Um, surely I covered that above? See the examples using:

$MyListX = $MyListX.unique;

More on .unique.

If you make a String into a Set (or a List) you end up with only one list item.

$MyStringX = "Green eggs and ham";
$MyNumber = $MyStringX.size; // 18 - the number of chars in above string

Now convert MyStringX from String-type to Set-type

$MyStringX = "Green eggs and ham";
$MyNumber = $MyStringX.size; // 1 - the number items in Set
// The set is a list of 1 item 'Green eggs and ham'

Are you asking the question you meant to ask? Hmm. Let’s take a string with repeated words

$MyStringX = "Green eggs and ham and tomatoes";
$MySet = $MyStringX.split(" ");

MySet now has 5 items Green;and;eggs;ham;tomatoes, note the duplicate and in the source string-made-into-a-list has been deleted. But:

$MyStringX = "Green eggs and ham and tomatoes";
$MyList = $MyStringX.split(" ");

The result is a list Green;eggs;and;ham;and;tomatoes. Note the dupe item! But if we add the action

$MyList = $MyList.unique;

Now the contents of MyList is de-duped: Green;eggs;and;ham;tomatoes

Sometimes just doing some small tests may be easier than imagining the output of something you’ve never yet tried. Of course, if you test in a new small file, there’s nothing to break if you mess up. :slight_smile:

2 Likes

Wow…thanks Mark for the explanation. Many items covered above on $String, $List and $Set conversions AND using a new operator (for me) .unique

Will put it into good use. Thank you very much and have a great rest of the week.
Tom

2 Likes