How to concatenate nested arrays of strings

From: Siegfried Heintze (siegfried_at_heintze.com)
Date: 12/28/04


To: <beginners@perl.org>
Date: Mon, 27 Dec 2004 18:36:13 -0700

I'm writing perl cgi and I like to buffer my output in an array of strings.
I just decided it would be very useful to be able to have nested arrays.

 

Below is a little function that does what a want. However,

(1) Is there a more compact way of performing this nested concatenation?

(2) Hasn't someone else already written a function like this that is
part of the standard repertoire?

(3) Assuming I have to use my own function below: My code only
concatenates the elements first argument. How can I concatenate all the
elements of all the function arguments?

(4) Is it possible to have nested lists? How would I modify my code to
handle those?

 

Thanks,

Siegfried

 

use strict;

use warnings;

sub concat {

  my $sResult = shift;

  return join("", map { ref($_) eq "ARRAY"? concat($_) :"$_ " }
@{$sResult});

}

print concat (["hello ", "there", ["a", "nested", "array"], "world!"]);



Relevant Pages

  • Re: Does VBscript support " & _" for long strings?
    ... With 1000 concatenations, the concatenation operator takes over 200 times longer than Join/Array. ... to an array element. ... Assigning a large string to an element in an array apparently is slightly faster than creating the same array using the Array function, but not so much that Array could be making superfluous copies of the string. ... Dim start, finish, i, total ...
    (microsoft.public.scripting.vbscript)
  • Re: How to concatenate nested arrays of strings
    ... > I'm writing perl cgi and I like to buffer my output in an array of strings. ... > Is there a more compact way of performing this nested concatenation? ... > concatenates the elements first argument. ...
    (perl.beginners)
  • Re: Windows gfortran binaries
    ... the concatenation here is an ... concatenate the elements of the array together. ... I was aware of vector subscripts, but conflated it with a recent ... Ken dot And dot Ann ...
    (comp.lang.fortran)
  • Re: Windows gfortran binaries
    ... "vector subscript", a very useful feature introduced in Fortran 90. ... the concatenation here is an ... concatenate the elements of the array together. ...
    (comp.lang.fortran)
  • Re: Want to reduce download time
    ... string builder functionality using arrays: ... Normal concatenation using '&' is *very* slow and if you do a lot of it will ... Using the array method is approx. ... what they are doing is making use of Dictionary objects ...
    (microsoft.public.scripting.vbscript)