Hi Prasad,
>>still does not work
>>$_ = "zzabtable";
>>/(.)(?!.*\1)/ && print "$1\n";
>>Output is z

The script above worked just has it was intended. The output should be 'z'
and not 't' as you suppose. Your code will match the first unique string
that is not repeated, after other letters. And in the case above 'z' is the
string not 't'.
if you use:
<code>
$_ = "zzabtablez";
/(.)(?!.*\1)/ && print "$1\n"; # output 't'
</code>
You should have 't' as intended, because 'z' was repeated in the string,
after others.The same reason
<code>
>>my $string = "abtable";
>>print $1 if $string =~ /(.)(?!.*\1)/; # print t
</code> worked.
Prasad, I don't know what you are getting at, but I suppose what you want is
the first occurrence of a unique string, that occur just once in the given
string. If my assumption is correct then try the code below and see if it
helps:
<code>
use strict;
use warnings;

my $string = 'thequickbrownfoxjumpovertheoldlazydog';
  my @arr=split //,$string;
  my %hash;
   foreach my $occur(@arr){  # use hash to get number of occurrence of each
letter
     if(exists $hash{$occur}){
        $hash{$occur}++;
     }else{$hash{$occur}=1}
   }my @arr_new;
  while(my($key,$value)=each %hash){
    push @arr_new,$key if $value==1; # get letter with only one occurrence
   }my @pos;
     for my $found(@arr_new){
       push @pos,index($string,$found); # get the index position of all the
letter with 1
                                                        # occurrence  in the
orignial string
     }
    @pos=sort {$a<=>$b}@pos;         # sort your index
    print substr($string,$pos[0],1);      # print out 'q'

</code>

Reply via email to