tags:

views:

95

answers:

4

How can I store entire contents of an array to a scalar variable. eg:

my $code = do { local $/; <FILE HANDLE>; };

This works fine for file handles but I need this for an array.

+10  A: 

Use join.

my $str = join '', @array;
friedo
This method also works swell if you want a quick way to separate the content of your string with line breaks for output, but note the double quotes. `my $str = join "\n", @array;`
Structure
+1  A: 

@arr = ("1","2","3") ;
my $arr = "@arr" ;
print "$arr";
Neeraj
A: 

You can actually use a scalar variable as a filehandle:

my $bigbuffer;
my $f;
open $f, ">", \$bigbuffer; # opens $f for writing into the variable $bigbuffer
# do whatever prints fwrites etc you want here
MarkR
You appear to have completely misread the question.
Michael Carman
+3  A: 

You can also take a reference to the array:

my @array = 'a'..'z';

my $scalar = \@array;

foo( $scalar );

sub foo {
    my $array_ref = shift;

    for my $f ( @$array_ref ) {

       do_something( $f ); 

    }
}

Which approach you take really depends on what you are trying to accomplish.

daotoad