tags:

views:

108

answers:

4

I was trying to join elements of a Perl array.

@array=('a','b','c','d','e');
$string=join(']',@array);

will give me

$string="a]b]c]d]e";

Is there anyway I can quickly get

$string="[a][b][c][d][e]";

?

+2  A: 

Perhaps:

{
  local $" = "][";
  my @array = qw/a b c d e/;
  print "[@array]";
}

Although you should probably just:

print "[" . join("][", @array) . "]";

Happy coding :-)

pst
Minor correction: The correct syntax would be: `print "[" . join("][", @array) . "]";`
steinar
@steinar Thanks for the correction -- switching between languages messed with my brain.
pst
+2  A: 
#!/usr/bin/perl
use strict; use warnings;

local $" = '';
my $x = qq|@{[ map "[$_]", qw(a b c d e) ]}|;

You can also generalize a little:

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

my @array = 'a' .. 'e';

print decorate_join(make_decorator('[', ']'), \@array), "\n";

sub decorate_join {
    my ($decorator, $array) = @_;
    return join '' => map $decorator->($_), @$array;
}

sub make_decorator {
    my ($left, $right) = @_;
    return sub { sprintf "%s%s%s", $left, $_[0], $right };
}
Sinan Ünür
@Sinan Ünür: Clearly you're doing something right, given your reputation on this site. However, do you really think this is a good solution to give someone who's clearly a new Perl programmer? Your solution uses a local predefined variable (that's not even scoped!), qq with nonstandard delimiters, embedded lists, anonymous arrays, and map.
A. Rex
Well, now you have string auto-increment, array references, anonymous subs (and thus closures), => instead of a comma, and sprintf. I assume you're just doing this for your own amusement at this point! (PS. There's no harm in that; it just doesn't help the OP.)
A. Rex
@A.Rex: Relax. Have some fun. The question has already been answered adequately.
Sinan Ünür
I'm glad I understood you correctly in the end! Your first solution just wasn't obvious enough to me, sorry; the second definitely helped. Take care.
A. Rex
Lots of people write `join STRING => LIST`; it's not all that weird.
tchrist
@A. Rex, @Sinan - This is showing someone that Perl is not boring while illustrating TMTOWTDI :) +1
DVK
+6  A: 

Here are two options:

#!/usr/bin/perl

use strict;
use warnings;

my @array = 'a' .. 'e';
my $string = join('', map { "[$_]" } @array);
my $string1 = '[' . join('][', @array) . ']';
mscha
+1. I will respectfully disagree with Sinan Ünür. This has the best-written solution posted so far, namely the join on "" of a mapped array. This is clearly distinct from the previous solution using qq|@{[..]}|.
A. Rex
I like the join/map solution.
tchrist
+7  A: 

Another way to do it, using sprintf.

my $str = sprintf '[%s]' x @array, @array;
FM
interesting, please explain why there's @array, @array
Matthew Lock
First array - due to being a second operand of `"x"` operator - is evaluated in scalar context and is thus # of elements in the array. The first expression them becomes: "[%s]" repeated N times (the meaning of string "x" operator". The second @array is just a list of parameters passed to sprintf
DVK