2017-09-20 141 views
-2
use strict; 
use warnings; 

#My sample bbl content 

my $sample = ' 
reflistStart 
\bibtype{Article}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Book}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Proceedings}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
'; 

$sample=~s#reflistStart((?:(?!reflist(?:Start|End)).)*)reflistEnd#my $fulcnt=$&; 
if($fulcnt=~m/\\bibtype\{article\}/i) 
{ 
    $fulcnt = ArticleReplacement($fulcnt); 
} 
elsif($fulcnt=~m/\\bibtype\{book\}/i) 
{ 
    $fulcnt = BookReplacement($fulcnt); 
} 
elsif($fulcnt=~m/\\bibtype\{proceedings\}/i) 
{ 
    $fulcnt = ProceedingsReplacement($fulcnt); 
} 
($fulcnt); 
#ges; 

sub ArticleReplacement 
{ 
    my $arttext = shift; 
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{articletitle\}/g; 
    return $arttext; 
} 
sub BookReplacement 
{ 
    my $arttext = shift; 
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{booktitle\}/g; 
    return $arttext; 
} 
sub ProceedingsReplacement 
{ 
    my $arttext = shift; 
    $arttext=~s/\\bibinfo\{title\}/\\bibinfo\{proceedingstitle\}/g; 
    return $arttext; 
} 

输出子功能:

reflistStart 
\bibtype{Article}% 
\bibinfo{articletitle}{Sample Title} #title changed as articletitle 
reflistEnd 
reflistStart 
\bibtype{Book}% 
\bibinfo{Booktitle}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Proceedings}% 
\bibinfo{Proceedingstitle}{Sample Title} 
reflistEnd; 

我在这里做的每个引用类型(文章,书籍,论文集)“IF”条件。因此,我的问题,有任何其他通过使用哈希例如:我怎么能重复使用哈希

my %Refstyles = ('article' => \&ArticleReplacement, 'book' => \&BookReplacement, ...); 
+2

我认为你需要解释你想要做的是什么。此外,'print $ Refstyles {$ 1}'将打印出类似于'CODE(0x1e8fea0)'的东西,并且根本不会调用子例程。 – Borodin

+0

更新了我的问题 – ssr1012

+3

不,即使在您编辑之后,您确实不清楚您要做什么或者遇到什么问题。 –

回答

3

好的。我想我明白。你的问题是这样的:

我有一些TeX文件有一个\bibtype{xxx}定义后跟\bibinfo{title}定义在下面的行。

我需要使用bibtype值更改bibinfo定义中的title以确定替换文本。

我想这样做使用子程序引用的散列,其中每个子例程更改一种类型的bibtype

这是否准确?

如果是这样,我认为你是过于复杂的事情。我认为你可以在没有子程序或引用或类似的东西的情况下做到这一点。我会写的是这样的:

#!/usr/bin/perl 

use strict; 
use warnings; 

# Hash that maps the bibtype to the title type 
my %subs = (
    article  => 'articletitle', 
    book  => 'booktitle', 
    proceedings => 'proceedingstitle', 
); 

# Turn the keys of that hash into a regex 
my $match = join('|', keys %subs); 

my $sample = ' 
reflistStart 
\bibtype{Article}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Book}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Proceedings}% 
\bibinfo{title}{Sample Title} 
reflistEnd 
'; 

# A slightly complex substitution operator. 
# We match all of the text that we're interested in (over two lines). 
# We capture the bibtype and then replace the title with the new 
# title string as looked up in our %subs hash. 
# Another slight cleverness, is the use of \K and (?=...) to match 
# bits of the string that we don't want to replace. See perldoc perlre 
# for more details. 
# Oh, and we use /g to change all of the titles in one go. 
$sample =~ s/bibtype\{($match)\}%\n\\bibinfo\{\Ktitle(?=\})/$subs{lc $1}/eig; 

print $sample; 

运行此给出以下的输出:

reflistStart 
\bibtype{Article}% 
\bibinfo{articletitle}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Book}% 
\bibinfo{booktitle}{Sample Title} 
reflistEnd 
reflistStart 
\bibtype{Proceedings}% 
\bibinfo{proceedingstitle}{Sample Title} 
reflistEnd 

它看起来我的权利。

+0

Woww ..很棒......我知道了。 – ssr1012