On 08/04/2013 01:44 AM, timothy adigun wrote:


OR some like to write it like so:
{
  local $" = '][';
  print '[', "@fruits", ']';
}

that is a poor idea. setting $" isn't needed to do a simple join.

but what no one has said is a more direct way to do this. you need map and join but it is much cleaner and easier to understand and without the bracket or precedence issues:

my @fruits = qw( apple pear banana ) ;
print join '', map "[$_]", @fruits ;

now you can see each element surrounded by [] and then joined into one string for printing.

you don't even need the join if you are just printing it but i put it there so you can use the same concept if you are going to save the string.

print map "[$_]", @fruits ;

that is very clear what is going on and should be the code used to print such a string. use the join '' form if you need to keep the string.

uri

--
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
http://PerlHunter.com

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to