# To find unique words in files: #!/usr/bin/perl use warnings; use List::Compare; # Module # To enter many files: my $entry; my @a; # Use of do-until loop: do {print"\n\n Press 1 to enter a new file or 2 to exit: \n"; # do LOOP $entry=; chomp ($entry); $file_no=0; if ($entry==1) {$file_no++; # if LOOP starts # Upload the files: print "\n Enter the filename: "; $file=; chomp $file; ############################ # open the file, or exit: ############################ unless ( open(FILE, $file) ) { print "Cannot open file \"$file\"\n\n"; exit;} @string = ; close FILE; $string=join(" ",@string); $string=~ s/\s//gi; push @all,$string; } # End of if LOOP } until ($entry==2); # End of do-until LOOP ############################################### ################################################ $fnum=0; $wnum=0; foreach $item (@all) {$fnum++; # 1st foreach LOOP for all files @file_words=(); # To empty array while ($item=~ /[A-Za-z].*?,/g) # While LOOP2 starts { $wnum++; $word=$&; $word=~ s/,//g; $word=~ s/\s//g; push @file_words,$word; } # while LOOP2 ends for all words in file $num_all_words=@file_words; print "\n File No. $fnum Total Words: $num_all_words Words are:\n"; print join ("\n",@file_words); print "\n\n\n"; push @s,\@file_words; } # foreach LOOP ends $ele_no=@s; print "\n\n Elements No.: $ele_no\n Final Array: @s\n\n"; $all=List::Compare->new(@s); # Use of module function @file1=$all->get_unique(0); @file2=$all->get_unique(1); @file3=$all->get_unique(2); print "\n\n Unique Words in Files:\n\n file1: @file1\n file2: @file2\n file3: @file3\n\n"; exit; #################### #### bat, cat, #### rat, bat, #### bat, dog, #### C:\Users\x\Desktop>m.pl Press 1 to enter a new file or 2 to exit: 1 Enter the filename: 1.txt Press 1 to enter a new file or 2 to exit: 1 Enter the filename: 2.txt Press 1 to enter a new file or 2 to exit: 1 Enter the filename: 3.txt Press 1 to enter a new file or 2 to exit: 2 File No. 1 Total Words: 2 Words are: bat cat File No. 2 Total Words: 2 Words are: rat bat File No. 3 Total Words: 2 Words are: bat dog Elements No.: 3 Final Array: ARRAY(0x2902f5c) ARRAY(0x2902f5c) ARRAY(0x2902f5c) Unique Words in Files: file1: file2: file3: #################### #### Elements No.: 3 Final Array: ??? Unique Words in Files: file1: cat file2: rat file3: dog