-
Yes, I will attach some sample text that caused this question. It actually came form aTBRef. I was trying to compare/summarize the different methods for Dictionaries, Lists/Sets etc in a Numbers spreadsheet. I initially tried to use tables but I needed to capture just 3 attributes only: $Name, $Text and $NoteURL into the spreadsheet of selected notes. Your script gave me this flexibility.
-
Yes, I would like to “clean” / “trim” any leading whitespace from the beginning and end of the text. In addition, much of the $Text in question has double lines and normal quotes within the text. Yes, ideally I would like to condense and remove all emply lines from the body of the text as well if possible. Lastly, I think, since the $Text has quotation marks within the text, it would be best to start the $Text value with a single quotation and end with a single quotation to define the boundaries.
-
I was experimenting with Numbers, I had not tried Excel. Many times, I find import, export from Numbers is easier but if it works in Excel, then fine as well.
Basically, what I wanted were 3 columns using your custom attibute script:
| Name | Text | NoteURL |
---- $Text in question from 4 notes -----
—$Text from Note 1 ----
Dictionary.add(itemDict)
This reads a Dictionary-type argument itemDict from which a key and a value are parsed.
$MyDictionary = $MyDictionary.add({apple:green});
Note that quotes are not needed around the key and value.
If key does not exist, that key is created with a value of value.
If key exists, key is given value value. This replaces any/all existing values for this key.
Assume $MyDictionary has no ‘apple’ key. Example:
$MyDictionary = $MyDictionary.add({apple:fruit});
The key ‘apple’ is added and now has value ‘fruit’
$MyDictionary = $MyDictionary.add({apple:green});
The key ‘apple’ now has a new value ‘green’. Now assume the ‘apple’ key has multiple values of ‘fruit;green;red’:
$MyDictionary = $MyDictionary.add({apple:pie});
Now the value is just ‘pie’ because an .add() operator replaces all existing value(s).
This operator is also equivalent to:
Dictionary[“key”] = “value”;
To add an additional value(s) to existing value(s), see Dictionary.extend().
To remove a key—and any value(s) it has—from the Dictionary, there is no operator but instead the key string is deleted using a minus operator: see ‘Deleting key:value pairs’ here.
As from v9.5.2, the .add() operator accepts quoted strings. The following expressions are equivalent:
$MyDictionary.add({1:able}}
$MyDictionary.add(“{1:able}”}
But do not use either of the following example syntax:
$MyDictionary.add({“1:able”}} WRONG!
$MyDictionary.add({“1”:“able”}} WRONG!
Using offset addresses within itemDict
Either the keyStr or the valueStr may need to be calculated variable, for instance the valueStr might need to be the value of $MyString(“Some note”) or in a loop, $MyString(loopVar). These cannot be resolved within the .add() operator but itemDict can be a variable. Thus, in a loop, rather than:
vDict = vDict.add({$Name(aState)+“:”+$Color(aState)}); WRONG!
use:
vList.each(aState){
// Dict.add() can't resolve attribute offset value
// so store the complete string in a string
// variable and pass a single var argument to .add()
var:string vPair = $Name(aState)+":"+$Color(aState);
vDict = vDict.add({vPair});
};
Legacy form (pre-v9.5.0)
Dictionary.add(keyStr, valueStr)
This sets a keyStr to the valueStr.
If keyStr does not exist, that key is created with a value of valueStr.
If keyStr exists, keyStr is given value valueStr. This replaces any/all existing values for this key.
Assume $MyDictionary has no ‘apple’ keyStr. Example:
$MyDictionary = $MyDictionary.add(“apple”,“fruit”);
The keyStr ‘apple’ is added and now has valueStr ‘fruit’
$MyDictionary = $MyDictionary.add(“apple”,“green”);
The keyStr ‘apple’ now has a new value ‘green’. Now assume the ‘apple’ key has multiple values of ‘fruit;green;red’:
$MyDictionary = $MyDictionary.add(“apple”,“pie”);
Now the value is just ‘pie’ because an .add() operator replaces all existing valueStr(s).
—$Text from Note 2 —
Dictionary.contains(keyStr)
The expression:
$MyDictionary.contains(key)
is true if the dictionary contains the designated key.
Note that keyStr is literal string and not a regex pattern (unlike the operator’s use chained with some other data types).
Dictionaries are case-sensitive, but Dictionary.icontains(key) is available for case-insensitive matching.
—$Text from Note 3 —
Dictionary.count()
Dictionary.count
A Number-type property. Returns the number of keys in the dictionary (by their nature all keys are discrete so there is not potential of duplication in the count). If:
$MyDictionary = {cat:animal; dog:animal; rock: mineral};
$MyNumber = $MyDictionary.count;
MyNumber is now 3.
Dictionary.count and Dictionary.size are interchangeable. Use which ever seems more intuitive.
—Bonus — Text from Note 4 ----
Dictionary.extend(itemDict)
This reads a Dictionary-type argument itemDict from which a key and a value are parsed. It adds the value string to the value(s) of a key.
If key does not exist, that key is created with a value of value.
If key exists, value is appended to key’s existing value(s).
Assume $MyDictionary has no ‘pear’ key. Example:
$MyDictionary = $MyDictionary.extend({pear:fruit});
The key ‘pear’ is added and now has value ‘fruit’. Next:
$MyDictionary = $MyDictionary.extend({pear:green});
The key ‘pear’ now has an additional new value ‘green’, but the overall value is now a list and the key:value pair is pear:fruit;green.
To set the new valueStr so it replaces all existing value(s) see Dictionary.add().
Note that quotes are not used around either/both the keyStr and valueStr. As from v9.5.2, the .extend() operator accepts quoted strings. The following expressions are equivalent:
$MyDictionary.extend({1:able}}
$MyDictionary.extend(“{1:able}”}
But do not use either of the following example syntax:
$MyDictionary.extend({“1:able”}} WRONG!
$MyDictionary.extend({“1”:“able”}} WRONG!
Dictionary.extend(keyStr, valueStr)
This adds the valueStr string to the value(s) of a keyStr.
If keyStr does not exist, that key is created with a value of valueStr.
If keyStr exists, valueStr is appended to keyStr’s existing value(s).
Assume $MyDictionary has no ‘pear’ keyStr. Example:
$MyDictionary = $MyDictionary.extend(“pear”,“fruit”);
The keyStr ‘pear’ is added and now has valueStr ‘fruit’. Next:
$MyDictionary = $MyDictionary.extend(“pear”,“green”);
The keyStr ‘pear’ now has an additional new valueStr ‘green’, but the overall value is now a list and the key:value air is pear:fruit;green.
To set the new valueStr so it replaces all existing value(s) see Dictionary.add().
Dictionary.extend(dictStr)
From v9.5.0, the Dictionary.extend() operator takes a single argument, a dictionary—using the new {} syntax of key:pair elements which will extend the current elements.
$MyDictionary = $MyDictionary.extend({pear:green});
Note that quotes are not used around either/both the keyStr and valueStr. As from v9.5.2, the .extend() operator accepts quoted strings. The following expressions are equivalent:
$MyDictionary.extend({1:able}}
$MyDictionary.extend(“{1:able}”}
But do not use either of the following example syntax:
$MyDictionary.extend({“1:able”}} WRONG!
$MyDictionary.extend({“1”:“able”}} WRONG!
Using offset addresses within itemDict
Either the keyStr or the valueStr may need to be calculated variable, for instance the valueStr might need to be the value of $MyString(“Some note”) or in a loop, $MyString(loopVar). These cannot be resolved within the .add() operator but itemDict can be a variable. Thus, in a loop, rather than:
vDict = vDict.extend({$Name(aState)+“:”+$Color(aState)}); WRONG!
use:
vList.each(aState){
// Dict.extend() can't resolve attribute offset value
// so store the complete string in a string
// variable and pass a single var argument to .extend()
var:string vPair = $Name(aState)+":"+$Color(aState);
vDict = vDict.extend({vPair});
};
NB: I probably picked the hardest text as a representative case since it has html attributes, quotes and spaces but if it works here, it will work in most other places.
Thanks Sumner
Tom