代码之家  ›  专栏  ›  技术社区  ›  Suman

使用Perl 6进行批量文本处理

  •  4
  • Suman  · 技术社区  · 7 年前

    我正在阅读Think Perl 6 by Laurent Rosenfeld和Allen B.Downey 最近这是一本很好的书。

    它的.tex文件在github中可用 here .

    它有如下代码示例: enter image description here

    这样地: enter image description here

    为此,我们必须批量处理上述存储库中包含的所有.tex文件。

    \begin{verbatim}
            say 42 ==  42;           # True
            say 42 ==  42.0;         # True
            say 42 ===  42;          # True
            say 42 === 42.0;         # False
    \end{verbatim}
    
    
    \begin{verbatim}
    $x eq $y            # $x is string-wise equal to $y
    $x ne $y            # $x is string-wise not equal to $y
    $x gt $y            # $x is greater than $y (alphabetically after)
    $x lt $y            # $x is less than $y (alphabetically before)
    $x ge $y            # $x is greater than or equal to $y
    $x le $y            # $x is less than or equal to $y
    $x eqv $y           # $x is truly equivalent to $y
    \end{verbatim}
    

    \begin{minted}{perl6}
            say 42 ==  42;           # True
            say 42 ==  42.0;         # True
            say 42 ===  42;          # True
            say 42 === 42.0;         # False
    \end{minted}
    
    
    \begin{minted}{perl6}
    $x eq $y            # $x is string-wise equal to $y
    $x ne $y            # $x is string-wise not equal to $y
    $x gt $y            # $x is greater than $y (alphabetically after)
    $x lt $y            # $x is less than $y (alphabetically before)
    $x ge $y            # $x is greater than or equal to $y
    $x le $y            # $x is less than or equal to $y
    $x eqv $y           # $x is truly equivalent to $y
    \end{minted}
    

    以下是我的计划。

    THIS IS DUMMY CODE
    
    # First I want to skim all the .tex files in the cloned repo (with git) 
    
    for dir("ThinkPerl6/book") ->$file {
      say $file if $file~~/\.tex/;
    }
    
    # Read each .tex file and modify, replace `\begin{verbatim}` with `\begin{minted}{perl6}`
    
    for "$file.tex".IO.lines -> $line {
      substitute with "\begin{minted}{perl6}" if $line ~~/\\begin\{verbatim\}/;
    }
    
    # Read each .tex file and modify, replace `\end{verbatim}` with `\end{minted}`
    
    for "$file.tex".IO.lines -> $line {
      substitute with "\end{minted}" if $line ~~/\\end\{verbatim\}/;
    }
    

    我无法超越这一点。有什么帮助吗?使用regexp将非常有用。

    苏曼

    2 回复  |  直到 5 年前
        1
  •  6
  •   moritz    7 年前

    您需要执行以下步骤:

    • 创建每行的副本,并应用替换。您可以使用 subst method 为此
    • 将修改后的副本写入新文件(可能具有扩展名 .new 添加左右)
    • 覆盖原始文件。看见 this example 寻求灵感。

    我希望这有帮助。

        2
  •  4
  •   Christopher Bottoms zerkms    5 年前

    my $fh-out = open "$file.new.tex", :w; # Create a new file
    
    # Read in old file, line by line
    for "$file.tex".IO.lines -> $line is copy {
    
        # Make changes, if needed
        $line.=subst('\begin\{verbatim\}','\begin{minted}{perl6}');
        $line.=subst('\end\{verbatim\}','\end{minted}');
    
        # Print line to new file
        $fh-out.put: $line;
    }