zli034 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, this's just a obvious question, I be pleased if anyone can make this code better for me.

sub return_string{ my @temp = (12,23,34,4,5); my $seq; foreach my $key(@temp){ $seq = $seq.$key.','; } return substr($seq, 0, -1); }

This's just an arry, I want to return it as comma seperated string.

must be quicker way to do it

Replies are listed 'Best First'.
Re: Return a comma seperate array
by FunkyMonk (Bishop) on Feb 19, 2008 at 23:23 UTC
    Have a look at join:
    sub return_string{ my @temp = (12,23,34,4,5); return join ",", @temp; }

    update: Honestly, I didn't copy runrig :)

Re: Return a comma seperate array
by runrig (Abbot) on Feb 19, 2008 at 23:22 UTC
    Have a look at join.
Re: Return a comma seperate array
by lodin (Hermit) on Feb 19, 2008 at 23:58 UTC

    TMTOWTDI:

    my @array = qw/ foo bar baz /; my $str = do { local $" = ','; "@array" }; print $str; __END__ foo,bar,baz
    But really, use join.

    lodin