splitting strings, using ranges

Get help using and writing Nisus Writer Pro macros.
Post Reply
credneb
Posts: 148
Joined: 2007-03-28 07:30:34

splitting strings, using ranges

Post by credneb » 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]

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: splitting strings, using ranges

Post by martin » 2009-03-13 11:56:13

Hi Cliff,
credneb wrote: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
If you see the same prompt as me you will notice it says "the location is ()", which does not confirm that $loc is a Range as you expect. Rather, the empty parentheses stand for an empty array. If you ever need to confirm the type of a variable you can use:

Code: Select all

$type = TypeOf $loc
Prompt $type
The problem is this line:

Code: Select all

$loc = $sel.rangeOfString(' '), b
The parentheses must surround all arguments to the command. Also strictly speaking, a plain b without any quotation marks is illegal. I'm not sure why this line doesn't trigger an error actually, but it should be:

Code: Select all

$loc = $sel.rangeOfString(' ', 'b')
I'll file a bug or two on this line of code. As to the glossary breaks:
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?
The only solution I can recommend is the menu command Insert > Glossary Entry Break. There is a special Nisus-only RTF code for the glossary break, but I wouldn't rely on it.

PS: if you want to use "code" tags in your forum post, make sure you have the "Disable BBCode" option unchecked. You can do this on a per-post basis, or enable it in your user profile.

credneb
Posts: 148
Joined: 2007-03-28 07:30:34

Re: splitting strings, using ranges

Post by credneb » 2009-03-13 13:51:40

Hi Martin,

Thank you again.

Perhaps there is another bug to report. Before changing anything, I ran the macro again and the Prompt message following the

$loc = $sel.rangeOfString(' '), b

line returned "the location is (Range<5,1>), the same message I now get with the corrected syntax and why I thought it was correct.

For reference, I am running 10.4.11 on a Powerbook G4 (which is why I'm trying to get my essential macros ported to Nisus Pro...)

Cliff Bender

ps. Thank you for the ps. I was wondering why the code didn't post as I had seen others.

credneb
Posts: 148
Joined: 2007-03-28 07:30:34

Re: splitting strings, using ranges

Post by credneb » 2009-03-13 14:14:26

Perhaps I should note that I was testing by selecting the string

hello 1, dolly 2, hello dolly 3,

and the first run through showed that the selection was "hello 1" so the range <5,1> should have been OK (as it is now).

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: splitting strings, using ranges

Post by martin » 2009-03-13 14:31:54

credneb wrote:Perhaps there is another bug to report. Before changing anything, I ran the macro again and the Prompt message following the

$loc = $sel.rangeOfString(' '), b

line returned "the location is (Range<5,1>), the same message I now get with the corrected syntax and why I thought it was correct.
Thanks for the extra information, but I think I can explain this. The $loc variable is still an array, simply an array containing a single Range. If $loc was just a Range it would show in the prompt as "Range<5,1>" instead of "(Range<5,1>)". The subtle difference is the inclusion of parentheses, which is the cue that it's an array.

When I first ran your macro my selection did not include a space character. Thus the "rangeOfString" command was returning the Undefined macro value, which always prints as completely empty, which is why my array showed more definitively as "()".

Post Reply