English 中文(简体)
珀尔·约翰以不同的方式出现?
原标题:Perl JOIN in a different way?

我正在研究一个 perl 模块, 寻找窗体的输出( 字符串) : < code> a: value1 OR a: value2 OR a: value3 OR...

数值 value1, value2, value3... 在数组中( says, @values) 。

我知道我们可以使用 join( OR, @values) 来创建窗体的连接字符串: value1 ORvalue2 ORvalue3 OR...

但如上所述,我还需要一个额外的 < code> a: 来为每个值预设。

这样做有什么好办法?

最佳回答

您通常使用 < a href=> "http://perldoc.perl.org/forms/map.html" rel="noreferrer">map 来做这些事情:

#!/usr/bin/env perl
use strict;
use warnings;

my @array = qw(value1 value2 value3);
print join(" OR ", map "a:$_", @array),"
";

产出:

a:value1 OR a:value2 OR a:value3

map 是一个简单的循环构造, 如果您想要对列表的每个元素应用一些简单的逻辑, 而不对您的代码做太多的杂乱, 这会有用 。

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签