splitting strings, using ranges
Posted: 2009-03-13 10:39:37
Hello again.
I've split a string of text into an array, and then for each element in the array I want to extract the part following the last space to use as a glossary abbreviation, and the entire string (incl the abbrev) as the expansion. I guess using ranges is the way to do this splitting.
Prompts confirm that everything is OK until the line
$break = $loc.location
where an error occurs with the message "Array object does not have a bound property." But the preceding line just confirmed that $loc is a Range, which has the bound property, so $break is a new var that should become the index to the character after the space (no?) (I tried with the location property, to the same end)
Obviously I'm doing something wrong, but can't figure it out.
Separately, the idea is to build a text block that gets pasted into a glossary file, so the last \n will need to be changed to a Glossary Entry Break. Is there is a command or character code that can be inserted here to do this, or after figuring out my string problem rethink to insert the entries one at a time using the menu command in the target glossary file?
Thank you.
Cliff Bender
[code]$doc = Document.active
$source = $doc.selectedSubtext
Select End
$items = $source.split(', ')
$pasteText = ''
ForEach $sel in $items
$i = Prompt 'the selection is ' & $sel
$len = $sel.length
$i = Prompt 'the length is ' & $len
$loc = $sel.rangeOfString(' '), b
$i = Prompt 'the location is ' & $loc
$break = ($loc.bound)
$range = Range.new($break,$len-$break)
$i = Prompt 'the range is ' & $range
$abbrev = $sel.subtextInRange($range)
$i = Prompt 'the abbreviation is ' & $abbrev
$pasteText &= "$abbrev\n$sel\n"
End
Insert Text $pasteText
[/code]
I've split a string of text into an array, and then for each element in the array I want to extract the part following the last space to use as a glossary abbreviation, and the entire string (incl the abbrev) as the expansion. I guess using ranges is the way to do this splitting.
Prompts confirm that everything is OK until the line
$break = $loc.location
where an error occurs with the message "Array object does not have a bound property." But the preceding line just confirmed that $loc is a Range, which has the bound property, so $break is a new var that should become the index to the character after the space (no?) (I tried with the location property, to the same end)
Obviously I'm doing something wrong, but can't figure it out.
Separately, the idea is to build a text block that gets pasted into a glossary file, so the last \n will need to be changed to a Glossary Entry Break. Is there is a command or character code that can be inserted here to do this, or after figuring out my string problem rethink to insert the entries one at a time using the menu command in the target glossary file?
Thank you.
Cliff Bender
[code]$doc = Document.active
$source = $doc.selectedSubtext
Select End
$items = $source.split(', ')
$pasteText = ''
ForEach $sel in $items
$i = Prompt 'the selection is ' & $sel
$len = $sel.length
$i = Prompt 'the length is ' & $len
$loc = $sel.rangeOfString(' '), b
$i = Prompt 'the location is ' & $loc
$break = ($loc.bound)
$range = Range.new($break,$len-$break)
$i = Prompt 'the range is ' & $range
$abbrev = $sel.subtextInRange($range)
$i = Prompt 'the abbreviation is ' & $abbrev
$pasteText &= "$abbrev\n$sel\n"
End
Insert Text $pasteText
[/code]