Sometimes it is useful to find the index of every occurrence of a given character in a string. Let's say, for example, that you're trying to find doublequoted strings which may contain backslash-escaped quote characters. An easy way of doing this is..: # find the index of every doublequote character; # perform a values-to-keys inversion on the preceding array so that you then have a list of positions that point to sequence numbers; # find the index of every backslash character; # for each backslash, if the index + 1 of that backslash is in the list of doublequote positions, mark position for skipping; # iterate through the list of all doublequotes, skipping the marked ones. Each odd-numbered index indicates the beginning of a doublequoted string and each even index, the end of one. {{{ awk function findallindices(str, chr, arr, i, j) { for (i = 1; i <= length(str); i++) if (substr(str, i, 1) == chr) arr[++j] = i return j } }}} The function returns the number of instances found as a convenience (gsub() is a faster way of accomplishing this if that's all that is needed). The *arr* argument is expected to be an array in which all of the indices are accumulated. It is deliberately not zeroed out so that it can be filled with indices of different characters by repeated invocation. The escaped-doublequote example was inspired by a common APL tactic.
Summary:
This change is a minor edit.
Username: