Advertisement
Codeprasa

searching specific word using abbreviated keyword

May 11th, 2025 (edited)
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.49 KB | Source Code | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. #use diagnostics;
  6. use v5.40.0;
  7.  
  8. my $fh;                                                         #this is the file handler
  9. my $source = 'sample.txt';                                      #this is the source file                                   
  10. my $des;                                                        #once the targeted word(s) fetched, stores them in this variable
  11. my $destination = "output.txt";                                 #new found words are stored in this text file
  12. my $string;                                                     #this is the targeted string to be searched
  13. my $word;
  14. my $keywords_with_keys;
  15. my $keywords_with_values;
  16. my $keyword;
  17.  
  18. #my @keywords_with_keys;
  19. #my @keywords_with_values;
  20.  
  21. my %keyword = (adj => 'adjective',
  22.                adv => 'adverb',
  23.                dat => 'dative',
  24.                fem => 'feminin',
  25.                con => 'conjunction',
  26.                mas => 'masculine',
  27.                neu => 'neutral',
  28.                nou => 'noun',
  29.                plu => 'plural',
  30.                ref => 'reflexive verb',
  31.                ver => 'verb');
  32.                
  33.  
  34.  
  35.  
  36. #my @avoid_letters = ("a", "b", "c");
  37.  
  38. open($fh, '<', $source) or die $!;
  39.  
  40. open($des, '>', $destination) or die $!;
  41.  
  42.     sub abbreviated_list{
  43.         for(keys %keyword){
  44.             $keywords_with_values = ucfirst $keyword{$_};
  45.             $keywords_with_keys = ($_);
  46.             say $keywords_with_keys, " - ", $keywords_with_values;
  47.         }
  48.     }
  49.  
  50. print "You can use the abbreviated keywords.\n";       
  51.         abbreviated_list();
  52.    
  53. print "\nEnter the keyword:";
  54. chomp($word = <STDIN>);
  55.     my $safe_search = quotemeta($word);
  56.     while($string = <$fh>){
  57.         if($string =~ /\b($safe_search)\b/i) {
  58.             print $des ($string);
  59.         }
  60.     }
  61.    
  62. close($fh);
  63. close($des);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement