Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Array to String

by amexmythili (Novice)
on Jan 26, 2009 at 14:39 UTC ( #738944=perlquestion: print w/replies, xml ) Need Help??
amexmythili has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

Any body help me to know this.
I want to split an array in to string. iam having an array like this.
@array=(balack, white, green, red);
iam want to insert some tagging in starting and ending of the each element and store it in a string.

for example:
<tag>black</tag>, <tag>white</tag>, <tag>green</tag>, <tag>red</tag>
Any body help me in this.
Mythili B

Replies are listed 'Best First'.
Re: Array to String
by Corion (Pope) on Jan 26, 2009 at 14:44 UTC

    See join for joining array elements into a string.

    See map for converting array elements into other.

    Basically, your conversion will look like this:

    my @array = qw(balack white green red); my @tagged_array = map { ... } @array; my $result = join ... @tagged_array; print $result;

    Of course, I've left out the interesting parts, but as you don't show the code you've written, I can't help you more.

Re: Array to String
by moritz (Cardinal) on Jan 26, 2009 at 14:45 UTC
    join ", ", map "<tag>$_</tag>", @array;

    For reference, see the documentation of join and map.

Re: Array to String
by jethro (Monsignor) on Jan 26, 2009 at 14:45 UTC
    foreach (@array) { $_= '<tag>' . $_ . '</tag>' }
    or with map and creation of a new array
    my @newarray= map { '<tag>' . $_ . '</tag>' } @array;

    UPDATE: misread your question. Should you ever need the tags in the array itself, this would be the answer ;-)

Re: Array to String
by explorer (Chaplain) on Jan 26, 2009 at 17:40 UTC

    Do you like XML?

    use XML::Simple; my @array = qw(balack white green red); print XMLout(\@array, RootName => undef, NoIndent => 1); __OUTPUT__ <anon>balack</anon><anon>white</anon><anon>green</anon><anon>red</anon +>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://738944]
Approved by jethro
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-11-19 00:21 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (278 votes). Check out past polls.