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

Embedding a Perl filter
https://www.nisus.com/forum/viewtopic.php?f=17&t=3153
Page 1 of 1

Author:  js [ 2008-08-28 00:56:07 ]
Post subject:  Embedding a Perl filter

To sort paragraphs and kill duplicate lines I normally use a Perl macro like this

Code:
use strict;
my %seen;
while (<>) {
    $seen{$_}++;
}
print sort keys %seen;


I tried to integrate this into a Nisus macro like this:
Code:
Begin Perl
my perl macro
End

But this does not work. What should I have done?
Is there maybe a Nisus macro which does the same thing?

Author:  Kino [ 2008-08-28 03:51:51 ]
Post subject:  Re: Embedding a Perl filter

If you don't need exchanging data between a Perl block and other parts of the same Nisus macro and if you just want to replace a selection with the output of your perl script, then I think you'd better write it as a Perl macro (with pl as file extension), not as a Nisus macro.
Code:
#Nisus Macro Block
#source front selection
#destination clipboard
#after execution
#Paste
#End Nisus Macro Block

my %seen;
while (<STDIN>) {
   $seen{$_}++;
}
print sort keys %seen;

js wrote:
Is there maybe a Nisus macro which does the same thing?

The simplest Nisus Writer Pro way would be...
Code:
Menu ':Edit:Sort Paragraphs:Ascending (A-Z)'
Replace All '(^.*\n)\1+', '\1', 'E-is'

Author:  js [ 2008-08-28 12:29:26 ]
Post subject:  Re: Embedding a Perl filter

Thank you, Both work fine now.

I found BTW that in the case of the Perl macro, even without using it with additional Nisus code, I had to add a Start Perl / End line to make it work.

Author:  Kino [ 2008-08-29 04:24:45 ]
Post subject:  Re: Embedding a Perl filter

js wrote:
I found BTW that in the case of the Perl macro, even without using it with additional Nisus code, I had to add a Start Perl / End line to make it work.

That means that you saved it as a Nisus Macro (filename.nwm). If it were a Nisus Perl Macro (filename.pl), "Begin Perl ... End" should have yielded an error.

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