Change_Item Node
Change_item replaces, adds, deletes, or alters an item in a list. It saves you the trouble of slicing up a list and then recombining it just to make a simple change.
And, importantly, it guarantees that the new item is the same type (integer, number, string, etc.) as the old one. Type mismatches are surprisingly easy to make, can be hard to spot, and can cause mysterious trouble down the line. If you have a list of integers with one string or number that LOOKs like an integer, and you try to take a sum, you will get a perplexing error message. This will not happen when you use change_item.
And you can use change_item with most any kind of list: strings, colors, points, integers, numbers, booleans, geometries, paths, even data tables!
Change_item takes six parameters:
- List. A list of items.
- Option. Six kinds of operation:
- Replace. Replace one item with another of the same type.
- Add Before. Add a new item before the given position in the list.
- Add After. Add a new item after the given position in the list.
- Delete. Delete one item from the list.
- Nudge. Change the position of an item; only works with geometries, paths, or points (ignored in other kinds of lists).
- No Change. Leave the list unchanged. Handy when doing before/after checks.
- Index. The position in the list to be acted on. Cannot be less than 0. An index greater than the size of the list will be treated like the final item.
- New Value. The new value to inserted into the list. Can be a color, string, integer, number, boolean, or a point. If left empty, a placeholder may be inserted (see below). Values entered directly into the New Value field will be converted to the proper type (e.g. a hex string will be converted to an actual color, "true" or "false" or 0 or 1 or 1.0 will be converted to an actual boolean, etc. Numbers entered to replace integers will be rounded.
- New Shape or Data. A path, geometry, or data table row which can not be entered into the new value field can be entered in this port instead. Although the port will accept more than one item, only the first one will be used. Ignored if the list is not one of those three types.
- Nudge. An x, y position used to offset an item if Nudge is selected as the option. Ignored if the item being nudged is not a path, geometry, or point. Handy for doing spot kerning with my charpath node.
Change_item tries to keep lists homogeneous and error free even as you are editing them. If you ask change_item to replace a particular item but fail to provide a new value, or try to insert a type of value that cannot be converted to match the type of the replaced item, change_item will try to insert a placeholder of the correct type until you can provide a proper replacement value.
Placeholders are also helpful when finding the correct index. You can render the output of the change_item and watch the placeholders move through the list as you increment the index until you find the correct position (even before deciding what to replace it with). The placeholder used depends on the type of list.
Placeholders used for missing or mismatched replacements by type:
- Color. Black. (If you try to replace a particular color with, say, the number pi, you will just get a black color.)
- String. Null value (nothing)
- Integer. Zero (0)
- Number. Zero (0.00)
- Boolean. false
- Point. 0.00,0.00
- Path. If missing, a bounding box of the item to be replaced. If mismatched with a non-path, the mismatched item.
- Geometry. Same as path.
- Data Table. If missing, a valid row (with the same columns as other rows) with question marks in each cell. If mismatched with anything other than a data row, the mismatched item (which will show as null or not found values in the cells).
Note: In general you can put any type of value into either the New Value or New Shape or Data and change_item will try to do the appropriate conversion or fail gracefully. But if you try to feed a data table row into the New Value port, you will trigger an error message.
As you can see, this node includes a lot of circuitry to handle type conversions. But this is not anything you have to worry about. Change_item handles all this so that you don't have to think about it.
The attached demo shows a whimsical typographical design made using a number of change_item nodes:
- Change_item1 helps remove the bar from the capital A by adding an additional point to the outer contour of the character.
- Change_item2 replaces the original A with the modified A.
- Change_item3 changes the fifth color in a list of current letter colors (used to turn the letter I green).
- Change_item4 replaces the letter I with a scaled character.
- Change_item5 through 8 kerns individual characters.
Feel free to play with this example by changing the text, font, font size, etc. to create other logos or playful designs. If you make significant changes at the top you will probably need to make some adjustments below, but this can often be done quickly and easily.
The demo also contains a test bench to the right of the main network. You can use this to create lists of different types (colors, numbers, points, balloons, etc,), then feed in different replacement values, shapes, or data to see what happens.
Change_item uses my recently published identify node to do all its internal type checking. Before I had to do replacements in lists by hand, reinventing the wheel each time. Identify finally allowed me to create a truly generic node that just works, regardless of what you throw at it.
Enjoy!
-
change_item_screenshot.png 466 KB
- change_item_demo.ndbx.zip 240 KB
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac