将数组转换为字符串

时间:2014-08-05 15:02:30

标签: arrays perl

好的,所以我有一个看起来像这样的数组:

@foo = ("a","b","c","d");

...和一个存储在变量中的字符串:

my $foo = "e";

我想把它变成一个看起来像这样的字符串:

"e/a;e/b;e/c;e/d"

换句话说,我想添加" $ foo /"到每个数组元素的开头并将其转换为由分号分隔的字符串。我怎么能这样做?

谢谢!

1 个答案:

答案 0 :(得分:10)

mapjoin

use warnings;
use strict;

my @foo = ("a","b","c","d");
my $foo = "e";
my $s = join ';', map { "$foo/$_" } @foo;
print "$s\n";

__END__

e/a;e/b;e/c;e/d