Embedded Perl macro and list variables

Get help using and writing Nisus Writer Pro macros.
Post Reply
Nobumi Iyanaga
Posts: 151
Joined: 2007-01-17 05:46:17
Location: Tokyo, Japan
Contact:

Embedded Perl macro and list variables

Post by Nobumi Iyanaga » 2008-12-20 06:45:14

Hello,

I have a question about the embedded Perl scripts:
We read in the Macro Reference: "Scalar variables (integers, strings, etc) that have been defined by your macro are automatically made available to the Perl code. Array and hash variables are also available." But how can we have access to array or hash variables in the embedded Perl script? I have two array variables, containing one an array of ranges, and the other an array of attributes, that I get using this code:

Code: Select all

$charIndex = 0 
$limit = $text.length
$ranges = Array.new
$attributes_array = Array.new

# inspect the attributes applied to every character in the text 
While $charIndex < $limit 
	$range = $text.rangeOfDisplayAttributesAtIndex($charIndex) 
	$ranges.push ($range)
	$attributes = $text.displayAttributesAtIndex($charIndex) 
	$attributes_array.push ($attributes)

	# move to the next area that has different attributes 
	$charIndex = $range.bound 
End 
I would like to process these arrays in an embedded Perl script, if possible like this:

Code: Select all

Begin Perl
	@ranges = $ranges; # the Nisus variable "$ranges"
	@attributes_array = $attributes_array;  # the Nisus variable "$attributes_array"

	for (@ranges) {
		....;
	}
	for (@attributes_array) {
		....;
	}
	....
End
But the two array variables are in fact not simple arrays of scalar values... Anyway, it seems that this code doesn't work.

I would appreciate any insight. Thank you in advance.
Best regards,

Nobumi Iyanaga
Tokyo,
Japan

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Embedded Perl macro and list variables

Post by Kino » 2008-12-20 21:04:41

Nobumi Iyanaga wrote: We read in the Macro Reference: "Scalar variables (integers, strings, etc) that have been defined by your macro are automatically made available to the Perl code. Array and hash variables are also available." But how can we have access to array or hash variables in the embedded Perl script?
...
I would like to process these arrays in an embedded Perl script, if possible like this:

Code: Select all

Begin Perl
	@ranges = $ranges; # the Nisus variable "$ranges"
	@attributes_array = $attributes_array;  # the Nisus variable "$attributes_array"
	....
End
The followings of your code are unnecessary and very likely to prevent your macro from working properly. If it is an array, $name is converted to @name autumatically when sent to a perl block.

Code: Select all

	@ranges = $ranges;
	@attributes_array = $attributes_array;

Nobumi Iyanaga
Posts: 151
Joined: 2007-01-17 05:46:17
Location: Tokyo, Japan
Contact:

Re: Embedded Perl macro and list variables

Post by Nobumi Iyanaga » 2008-12-21 04:37:44

Hello Kino,

Thank you for your reply.
Kino wrote: The followings of your code are unnecessary and very likely to prevent your macro from working properly. If it is an array, $name is converted to @name autumatically when sent to a perl block.

Code: Select all

	@ranges = $ranges;
	@attributes_array = $attributes_array;
You are right. I tried the following:

Code: Select all

$fruits = Array.new('cherry', 'watermelon', 'lime')
Debug.setIncludePerl true
Begin Perl
	for (@fruits) {
		print $_, "\n";
	}
End
And this works parfectly. -- But the code in my first post had another problem: the elements of the array variables are "objects", not scalar values. So I should make arrays of scalar values to pass to the Perl block. But I have to say that this is not so simple for the "attributes" that I get using the code:

Code: Select all

$attributes = $text.displayAttributesAtIndex($charIndex)
I think the simplest, and fastest way to do this would be to write the values of these arrays to a temporary file, as strings, and read that file using Perl. Anyway, I will try this way.

Thank you again.
Best regards,

Nobumi Iyanaga
Tokyo,
Japan

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

Re: Embedded Perl macro and list variables

Post by martin » 2008-12-29 17:32:00

Nobumi Iyanaga wrote:I think the simplest, and fastest way to do this would be to write the values of these arrays to a temporary file, as strings, and read that file using Perl. Anyway, I will try this way.
I don't know what you're doing, so perhaps that's easiest. However, if you do nothing, the objects in the array should be converted to some kind of string automatically. As an example, an attributes object looks like this when printed in Perl:

Code: Select all

Attributes<fontSize=12.0, fontName="Times New Roman", isFontSubstituted=0, isFontMissing=0, bold=0, italic=0, underline=0, strikethrough=0, superscript=0, characterCase=0, language=English - US, paragraphStyle=Paragraph Style "Normal", paragraphStyleName="Normal", firstLineHeadIndent=0.0, headIndent=0.0, tailIndent=0.0, paragraphAlignment=0, paragraphWritingDirection=0, paragraphSpacingBefore=0.0, paragraphSpacingAfter=0.0, lineHeightMultiple=1.0>

Post Reply