On Sun, 4 Aug 2013 12:59:29 +0800 (SGT)
*Shaji Kalidasan* <[email protected]> wrote:
> I am facing some difficulty using join to display the array elements
>
> Here is the code snippet
>
> [code]
> use strict;
> use warnings;
>
> my @fruits = qw/apple mango orange banana guava/;
>
> #print '[', join '][', @fruits;
> #print ']';
>
> print '[', join '][', @fruits, ']';
> best,
> [/code]
>
> [output]
> [apple][mango][orange][banana][guava][]
>
> [/output]
>
>
> How can I make the output to eliminate the last empty square brackets
> [] using a single print statement. I used two print statements as
> shown in the code snippet above (#lines are commented out)
>
> Any help is greatly appreciated.
Join is greedy and considers the last ']' as part of its list. Try:
print '[', join( '][', @fruits ), ']';
--
Don't stop where the ink does.
Shawn
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/