Turn an array into a string

Ok, so I have an array that looks like this:

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

      

... and the string stored in the variable as such:

my $foo = "e";

      

I want to turn this into a string that looks like this:

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

      

In other words, I would like to add "$ foo /" to the beginning of each element of the array and turn it into a semicolon-separated string. How can i do this?

Thank!

+3


source to share


1 answer


map and join



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

      

+10


source







All Articles