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

Dear Monks,

I would like to make a string out of an array. Is there a better way than:
foreach (@arr) { $str .= $_ ; }
Thanks in advance
Luca

Replies are listed 'Best First'.
Re: From Array 2 string
by Corion (Patriarch) on Nov 15, 2005 at 10:26 UTC

    How about stringifying the array:

    my $str = "@arr";

    Stringifying an array will use the current value of $" to separate the array elements. It defaults to a space (" ") - if you want newlines, tabs or an empty string (or semicolons or whatever), localize your change like this:

    my $str = do { local $" = ""; "@arr" };
Re: From Array 2 string
by wfsp (Abbot) on Nov 15, 2005 at 10:29 UTC
    You could change the question around:

    How could I join the elements of an array into a string.

    There is a hint in there somewhere! ;-)

Re: From Array 2 string
by Aristotle (Chancellor) on Nov 15, 2005 at 10:51 UTC

    See join. And make sure you take at least a gander over the rest of perlfunc so you know what’s there.

    Makeshifts last the longest.

Re: From Array 2 string
by Samy_rio (Vicar) on Nov 15, 2005 at 10:50 UTC

    Hi jeanluca, Try this,

    use strict; my @arr = qw(a b c d e f g h); my $str; $str .= shift(@arr) until (!(@arr)); print $str;

    Using Join :

    $str = join '', @arr;

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      That's a very strange example for join you've got going there. I'd guess the OP is just after...
      my @arr = qw(a b c d e f g h); my $str = join('', @arr);
      Am I missing something?
      ---
      my name's not Keith, and I'm not reasonable.
Re: From Array 2 string
by jonadab (Parson) on Nov 15, 2005 at 12:21 UTC

    Sure.

    $;=sub{$\};my@x=map{my($x,$y)=($;,$_);$;= sub{$x->().$y}}@arr;;my$str=($x[-1]->());

    Or you could just use join, but what fun is that?

      This is just the kind of code that gives PERL a bad reputation as a write-only language. PERL is dying because of posts like this. You should use existing code from CPAN instead of inventing your own convoluted methods:

      use Array::Autojoin; my $str=mkarray(@arr); print "$str";
      </sarcasm>(just in case ;-)

      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: From Array 2 string
by radiantmatrix (Parson) on Nov 15, 2005 at 16:19 UTC

    The equivalent to your code is, as others have pointed out:

    $str = join( '', @arr );

    Please read the docs on join. You could also do:

    { local $" = ''; # see perlvar $str = "@arr"; }

    Which might be faster. See perlvar. Also check out Data::Dumper - even if its format is not what you want, the code inside might be enlightening with regard to creating string representations of various variable types.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
      20 words: Rate stringify join stringify 155272/s -- -29% join 219556/s 41% -- 51 words: Rate stringify join stringify 83452/s -- -16% join 99407/s 19% -- 2000 words: Rate stringify join stringify 2736/s -- -3% join 2813/s 3% --

      They're about the same speed, but stringification has a much bigger overhead. Better off with join.

        Errm, what? At a rate of 155272/s, does it even matter for 99.99% of the apps out there? They’re going to be waiting for the harddrive, or the network, or the terminal, or whatever.

        Better off with whichever one is more readable.

        (Which is going to be join most of the time – but recommending it on the basis of performance is plainly silly.)

        Makeshifts last the longest.