Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.88 KB | None | 0 0
  1. # now it's your turn - please replace the pseudo code comments ### with Perl 6 ...
  2. #| edit a text file at the first matching line
  3. multi sub MAIN ('edit', $filename, *@search-terms) {
  4.  
  5.     my $matching-line-number = 0;
  6.  
  7.     # add some code of your own here to ...
  8.  
  9.     ### slurp in the lines of the file (hint: how is the config file read?)
  10.     my $contents = slurp $filename;
  11.  
  12.     ### join the search terms into a string
  13.     my $terms = @search-terms.join(' ');
  14.  
  15.     ### make a pattern from the string
  16.  
  17.     ### find the first line in the file that matches the pattern
  18.     loop (my $i = 0; $i < $contents.split("\n").elems; $i++) {
  19.         if $contents.split("\n")[$i].contains($terms) {
  20.             $matching-line-number = $i + 1;
  21.             last;
  22.         }
  23.     }
  24.  
  25.     # open your editor at the first line that matches
  26.     MAIN('edit', $filename, $matching-line-number);
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement