Reply to topic  [ 4 posts ] 
Embedding a Perl filter 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
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?


2008-08-28 00:56:07
Profile

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


2008-08-28 03:51:51
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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.


2008-08-28 12:29:26
Profile

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


2008-08-29 04:24:45
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 2 guests


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