
這個問題是這個問題的延伸列出目錄的問題。我遇到一個問題,我需要理解文本的原始部分。
電流輸出
你看到第二個問題沒有意義。它被包含在subsection
大動脈炎在風濕病學目錄:
\subsection{Takayasu arteritis}
\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}
Lorem ipsum.
\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}
目前的輸出是這樣優秀的程式碼
\section{Rheumatology}
\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}
\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}
但我希望它是
\section{Rheumatology}
\subsection{Takayasu arteritis}
\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}
\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}
我的建議是,如果小節中存在問題,則應包含小節。
我的虛擬程式碼
Look for the environment \begin{question}...\end{question}.
See the subsection of the question (it locates above the question).
If there is no subsection, leave blank.
If there are many questions within one subsection, put only one subsection.
特登的程式碼
#!/usr/bin/env bash
## avoid errors if a directory has no *tex files
shopt -s nullglob
directories=("Cardiology" "Rheumatology" "Surgery");
## Change this to set whichever options you want.
printf "%s\n%s\n" "\documentclass{YOURCLASS}" "\begin{document}"
for directory in ${directories[@]}
do
## Reset the counter, avoid empty sections.
c=0;
for file in "$directory"/*tex
do
let c++
[ "$c" -eq 1 ] && printf "\n%s\n" "\section{$directory}"
## Extract the wanted lines
perl -lne '$a=1 && print "" if /\\begin{question}/;
print if $a==1;
$a=0 if /\\end{question}/;' "$file"
echo ""
done
done
echo "\end{document}"
我認為這些行的邏輯應該改變
## Extract the wanted lines
perl -lne '$a=1 && print "" if /\\begin{question}/;
print if $a==1;
$a=0 if /\\end{question}/;' "$file"
echo ""
它使用正規表示式來尋找文件中的所有問題,並忽略問題的小節。
資料範例(與上一個案例略有不同!)
\subsection{Takayasu arteritis}
\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}
\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}
Fever of unknown origin can be used when you do not know what is causing the disease.
% Show cases in MedScape and ask class.
Aneurysms.
\subsubsection{Treatment}
\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}
\begin{question}
{When is the checkup of the Takayasu arteritis?}
Only once per year.
You could expect every month like normally in this kind of diseases.
But only once per year.
\end{question}
您可以在其中忽略應用程式中的小節。
你怎麼能寫出偽代碼?
Terdon 代碼中的錯誤 2014 年 10 月 14 日發現
導致錯誤的資料範例
\subsection{3}
A 55 y.o male says that for the last year ...
\begin{question}
{What is the pathogenetic mechanism of his complains?}
\end{question}
由 Terdon 的程式碼解析得到的結果完全相同,但這是錯誤的,因為這句話一個55歲的..不應該再出現在最終結果中。如果小節和正文之間有輸入,則正確輸出。然而,這不能這樣假設。
造成此錯誤的原因是 Windows 符號,已移動這裡。
答案1
我本來打算擴展我的原始程式碼,但現在已經到了直接用 Perl 重新實現所有內容要容易得多的地步:
#!/usr/bin/env perl
## This is the path to the target directories
my $path="/Users/Masi/Dropbox/";
## The target directories
my @directories=("Cardiology","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)) {
## Reset the counter
my $c=0;
## Skip any files that aren't .tex
next unless $file =~ /\.tex$/;
## Open the file
open(my $fh,"$path/$dir/$file");
while (<$fh>) {
## Get the subsection. $_ is the current line.
$sub="$_" and $n=0 if /\\subsection{/;
## If this line is a question
if (/\\begin{question}/) {
## If this counter is 0, this is the first file
## of this directory, so print the section
## Print the section name
if ($dd==0) {
print "\\section{$dir}\n\n";
$dd++;
}
## If this counter is 0, this is the first question
## of this subsection, so print the subsection line
print "$sub\n" if $n==0;
$n++;
## Increment the counter, we want these lines
$c++;
## And print
print;
}
else {
## Print lines if the counter is 1
print if $c==1;
## reset the counter to 0
print "\n" and $c=0 if /\\end{question}/;
}
}
print "\n";
}
}
請注意,這會忽略子小節,但很容易對其進行修改以包含它們。
答案2
terdon程式碼的擴充或改進基於此中的一些邏輯回答
#!/usr/bin/env perl
## This is the path to the target directories
my $path="/Users/Masi/Dropbox/";
chdir $path or die "cannot chdir '$path'";
## Iterate over the directories
foreach my $dir (
"Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology",
"Propedeutics", "Radiology", "Rheumatology", "Surgery"
)
{
my $dd=0;
## Read the current directory
opendir my $DIR, $dir or die "cannot opendir '$dir'";
## Find all files in this directory
while (my $file = readdir($DIR)) {
## Reset the counter
my $c=0;
## Skip any files that aren't .tex
next unless $file =~ /\.tex$/;
## Open the file
open(my $fh,"$path/$dir/$file");
while (<$fh>) {
## Get the subsection. $_ is the current line.
$sub="$_" and $n=0 if /\\subsection{/;
## If this line is a question
if (/\\begin{question}/) {
## If this counter is 0, this is the first file
## of this directory, so print the section
## Print the section name
if ($dd==0) {
print "\\section{$dir}\n\n";
$dd++;
}
## If this counter is 0, this is the first question
## of this subsection, so print the subsection line
print "$sub\n" if $n==0;
$n++;
## Increment the counter, we want these lines
$c++;
## And print
print;
}
else {
## Print lines if the counter is 1
print if $c==1;
## reset the counter to 0
print "\n" and $c=0 if /\\end{question}/;
}
}
print "\n";
}
closedir $DIR or die "cannot closedir '$dir'";
}
我在其中添加了一些錯誤管理。