Text and Data Manipulation
SenseTalk has strong text handling capabilities. Its chunk expressions, described in Chunk Expressions, provide a powerful and intuitive means of accessing and manipulating specific portions of a text string. In addition, there are a number of commands and functions for obtaining information about text, converting between text and other data formats, and manipulating text at a high level. The commands and functions for performing these actions are described in detail below.
Capitalized
Function
Behavior: The capitalized
function returns text with the first letter of each word capitalized.
Syntax:
{the} capitalized of stringFactor
capitalized( stringExpr )
Example:
put capitalized of "now and then" --> "Now And Then"
Related:
CharToNum
Function
Behavior: Returns the numeric code (in Unicode) representing the first character of its parameter.
Syntax:
{the} charToNum of textFactor
charToNum( textExpr )
Example:
put charToNum("a") --> 97
Related:
Delete
Command
Behavior: The delete
command deletes a chunk of text or one or more occurrences of a target text string within a container. In its simplest form, it will delete every occurrence of the target text, regardless of case. Other forms allow you to specify a chunk by its location, or to tell how many occurrences of a target string—or even to indicate a particular occurrence—to delete, and to specify exact case matching.
Delete Chunk
This form of the delete
command will delete any chunk of text (characters, words, lines, or text items) from within a value. The chunk can be specified with any chunk expression describing the part of container that should be deleted. See Chunk Expressions for a full description.
Syntax:
delete chunk [of | in] container
Example:
set sentence to "In a hole in the ground there lived a hobbit."
delete words 2 to 4 of sentence
put sentence --> "In the ground there lived a hobbit."
Example:
delete the first 2 characters of line 15 of output
Example:
delete the first line of file manual
Delete Text or Pattern
This form of the delete
command will delete a target text value or pattern (see Pattern Language) from within a string. This form of delete
includes a number of variations which let you specify how many occurrences of the target—or even to indicate a particular occurrence—to delete, and to specify exact case matching.
Exactly one of the options defining the targetTextOrPattern must be supplied, as well as the container where the deletions will occur. The case options are optional. The options used may be specified in any order, although only one option of each type can be given.
Syntax:
delete {Options}
Options:
[in | within | from] container
{all {occurrences of} | every {occurrence of} } targetTextOrPattern
{the} [first | last] howMany {occurrences of} targetTextOrPattern
{the} ordinalTerm {occurrence of} targetTextOrPattern
occurrence ordinalNumber of targetTextOrPattern
[with | considering] case
[without | ignoring] case
Example:
delete "#" from text -- this will delete every occurrence of "#"
Example:
delete the last "s" in word 3 of sentence
Example:
delete all <punctuation or whitespace> from phoneNumber
Example:
delete every occurrence of "ugly" in manual
Example:
delete the third "i" within phrase considering case
You must include the in container or within container option in such a delete command. The container can be any container, including a variable, a portion of a variable (using a chunk expression), or a text file. If container is a variable, its contents may be of any sort, including a list or property list. The delete
command will delete the indicated text, searching through all values nested to any depth within such containers.
You must include one of the targetTextOrPattern options in a delete command, to specify what will be deleted. Simply providing a targetTextOrPattern expression will cause the command to delete every occurrence of the value of that expression within container, so use this option cautiously. You may optionally precede targetTextOrPattern by all {occurrences of} or every {occurrence of} in this case if you like, which makes the impact clearer.
If the first or last options are used, howMany will specify the number of occurrences of targetTextOrPattern that should be deleted, starting at either the beginning or end of container respectively.
If ordinalTerm or ordinalNumber is given, only a single occurrence of targetTextOrPattern will be deleted. The ordinalTerm should be an ordinal number, such as first
, second
, third
, and so forth, or one of the terms middle
(or mid
), penultimate
, last
, or any
. The ordinalNumber should be an expression which evaluates to a number. If it is negative, the delete
command will count backward from the end of container to determine which occurrence to delete.
If considering case is specified, only occurrences of targetTextOrPattern within container that match exactly will be considered for deletion. The default is to delete any occurrence of targetTextOrPattern regardless of case.
The delete
command sets a result (as returned by the result
function) that indicates the number of occurrences that were deleted.
Delete Values
In addition to deleting a value by position (such as delete item 3 of myList
), you can delete specific values from a list. This is done using an each expression. For example, to delete all occurrences of the word "at" from some text you would say delete each word of myText which is equal to "at"
. The values to delete are selected using the where (or which or whose) clause of the each expression, which provides a great deal of flexibility. So in addition to deleting chunks that are equal to a given value, you can use almost any criteria to select the items to be deleted.
Syntax:
delete each chunkType [of | in] sourceValue {where conditionWithEach | which operator value | whose propertyCondition}
Example
delete each item of addressList whose zip code is 80202
Example
delete each line of testCases where the last item of each is "failed"
Example
delete each item of scores which is equal to zero
Example
delete each line of file "entries" which is empty
Example
delete each item of subscriberList whose expirationDate is earlier than yesterday
Related: