3

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

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

... and a string stored in a 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'd like to add "$foo/" to the beginning of each array element and turn it into a string separated by semicolons. How can I do this?

brian d foy
134k31 gold badges214 silver badges613 bronze badges
asked Aug 5, 2014 at 15:02

1 Answer 1

11

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";

Output:

e/a;e/b;e/c;e/d
answered Aug 5, 2014 at 15:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.