Reply to topic  [ 4 posts ] 
Embedded Perl macro and list variables 
Author Message

Joined: 2007-01-17 05:46:17
Posts: 145
Location: Tokyo, Japan
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.

_________________
Best regards,

Nobumi Iyanaga
Tokyo,
Japan


2008-12-20 06:45:14
Profile WWW

Joined: 2008-05-17 04:02:32
Posts: 400
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;


2008-12-20 21:04:41
Profile

Joined: 2007-01-17 05:46:17
Posts: 145
Location: Tokyo, Japan
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.

_________________
Best regards,

Nobumi Iyanaga
Tokyo,
Japan


2008-12-21 04:37:44
Profile WWW
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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>


2008-12-29 17:32:00
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software