-
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