조건을 사용하여 이 대체 항목을 작성하는 방법

조건을 사용하여 이 대체 항목을 작성하는 방법

이제 내 작업 디렉토리에서 다음 단어를 바꾸고 싶습니다.->~와 함께$\to$그 단어가 안에 있지 않다면말 그대로환경.

의사코드

다음 항목을 모두 교체합니다.->~와 함께$\to$다른 곳에서는 있지만 내부에서는 그렇지 않습니다.\begin{verbatim}...\end{verbatim}.

이것을 기반으로 한 내 Perl 의사 코드훌륭한 답변

#!/usr/bin/env perl

## The target directories
my @directories=("Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", "Propedeutics", "Radiology", "Rheumatology", "Surgery");


## Iterate over the directories
foreach my $dir (@directories) {
    my $dd=0;
    ## Read the current directory
    opendir (my $DIR, "$path/$dir");
    ## Find all files in this directory
    while (my $file = readdir($DIR)) {
        ## Skip any files that aren't .tex
        next unless $file =~ /\.tex$/;

        ## Open the file
        open(my $fh,"$path/$dir/$file");

        while (<$fh>) {
            if (/\\begin{verbatim}/) {                    
                # Skip the Verbatim environment # TODO how this?
                # Need to tell it that you one line by one until \end{verbatim} is met, after which go normally forward.
                while (!/\\end{verbatim}/) {
                    $dd++;
                }
                $dd++;
            }
            else {
                if ($dd==0) {
                    $dd++;       # I think we need this here to go one line after another
                }
                $string =~ s/->/$\to$/g;
            }
        }
        print "\n";
    }

while 루프의 가장 안쪽 의사코드에 대해 잘 모르겠습니다.

        while (<$fh>) {
            if (/\\begin{verbatim}/) {                    
                # Skip the Verbatim environment # TODO how this?
                # Need to tell it that you one line by one until \end{verbatim} is met, after which go normally forward.
                while (!/\\end{verbatim}/) {
                    $dd++;
                }
                $dd++;
            }
            else {
                if ($dd==0) {
                    $dd++;       # I think we need this here to go one line after another
                }
                $string =~ s/->/$\to$/g;
            }
        }

조건이 있는 의사코드를 어떻게 작성할 수 있나요?

답변1

쉘이 bash라고 가정하면 다음과 같이 한 줄로 작성할 수 있습니다.

perl -i.bak  -pe '
    /\\begin\{verbatim\}/../\\end\{verbatim\}/ or s/->/\$\\to\$/g
' {Cardiology,Pathophysiology,"Patology and Biopsy",Physiology,Propedeutics,Radiology,Rheumatology,Surgery}/*.tex

이는 {...}정규식 수량자이므로 중괄호를 이스케이프해야 합니다.


코드를 다음과 같이 작성하겠습니다.

my @directories=(
    "Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", 
    "Propedeutics", "Radiology", "Rheumatology", "Surgery"
);
chdir $path or die "cannot chdir '$path'";

foreach my $dir (@directories) {
    opendir my $DIR, $dir or die "cannot opendir '$dir'";
    while (my $file = readdir($DIR)) {
        my $filepath = "$dir/$file";
        next unless -f $filepath and $filepath =~ /\.tex$/;

        open my $f_in, "<", $filepath 
            or die "cannot open '$filepath' for reading";
        open my $f_out, ">", "$filepath.new"
            or die "cannot open '$filepath.new' for writing";

        while (<$fh>) {
            if (not /\\begin\{verbatim\}/ .. /\\end\{verbatim\}/) {                    
                s/->/\$\\to\$/g;
            }
            print $f_out;
        }

        close $f_in   or die "cannot close '$filepath'";
        close $f_out  or die "cannot close '$filepath.new'";

        rename $filepath, "$filepath.bak"  
            or die "cannot rename '$filepath' to '$filepath.bak'";
        rename "$filepath.new", $filepath
            or die "cannot rename '$filepath.new' to '$filepath'";
    }
    closedir $DIR  or die "cannot closedir '$dir'";
}

나는 계속해서 그것을 더 OO로 만들 것입니다:

use autodie qw(:io);
use Path::Class;

foreach my $dir (
        "Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology", 
        "Propedeutics", "Radiology", "Rheumatology", "Surgery"
) 
{
    my $directory = dir($path, $dir);
    while (my $file = $directory->next) {
        next unless -f $file and $file =~ /\.tex$/;
        my $f_out = file("$file.new")->open('w');
        for ($file->slurp) {
            /\\begin\{verbatim\}/ .. /\\end\{verbatim\}/  or s/->/\$\\to\$/g;
            $f_out->print;
        }
        $f_out->close;
        rename "$file", "$file.bak";
        rename "$file.new", "$file";
    }
}

관련 정보