nisus.com
https://www.nisus.com/forum/

Embedded Perl macro and list variables
https://www.nisus.com/forum/viewtopic.php?f=17&t=3265
Page 1 of 1

Author:  Nobumi Iyanaga [ 2008-12-20 06:45:14 ]
Post subject:  Embedded Perl macro and list variables

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:
$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:
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.

Author:  Kino [ 2008-12-20 21:04:41 ]
Post subject:  Re: Embedded Perl macro and list variables

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:
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:
   @ranges = $ranges;
   @attributes_array = $attributes_array;

Author:  Nobumi Iyanaga [ 2008-12-21 04:37:44 ]
Post subject:  Re: Embedded Perl macro and list variables

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:
   @ranges = $ranges;
   @attributes_array = $attributes_array;


You are right. I tried the following:

Code:
$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:
$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.

Author:  martin [ 2008-12-29 17:32:00 ]
Post subject:  Re: Embedded Perl macro and list variables

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:
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>

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/