#!/usr/bin/perl

#
# Finds unique phrases at STDIN & prints them to STDOUT.
# License is current GNU GPL.
# (c) Light Olli, <olli@digger.org.ru>, Сбт 05 Янв 2002 19:23:18.
#
# Accepts a file at STDIN, then checks strings & writes to STDOUT only 
# those sentences that seem to be unique ones w/ a $separator_string between
# each other. Each sentence should start w/ a $separator.
#
# related man's: man -a strfile ; man -a fortune; man -a perl
#
$debug=0;
$separator="%";
$separator_string="$separator\n";
my %phrases; 

# чтение исходных данных.
$separator_count=0;
while (<STDIN>)
{
    chomp; chomp;
    if ("$_" eq "$separator")
    {$separator_count++;next;
    }
    $str=$_;
    if ($phrases{$separator_count}) {$phrases{$separator_count}=join("\n",$phrases{$separator_count},$str);}
     else {$phrases{$separator_count}=$str;}
}

# сортировка на уникальность
foreach $key1 (keys %phrases)
{$nonuniq=0;
 foreach $key2 (keys %phrases)
  {if ($key1 eq $key2) {next;}
   if ("$phrases{$key1}" eq "$phrases{$key2}")
    {$nonuniq=1;}
  }
 if ($nonuniq) {$phrases{$key1}=undef;} 
}

# печать результата.
foreach $key (keys %phrases)
{if ($phrases{$key}){print "$separator\n$phrases{$key}\n";}
}
print  "$separator\n";
