assigning array to a scalar variable

V

venkatesh.naughty

Hi all,
How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2
instead to store hi venkatesh
 
J

John W. Krahn

Hi all,
How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2
instead to store hi venkatesh

Use the reference operator \.

my $x = \@array;

This will give $x a reference to the array @array so if you modify an
element of the array from $x it will also modify the original array's data.



John
 
O

Oliver Abram

Hi all,
How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2
instead to store hi venkatesh


Or maybe this one:


@array = qw (hi venkatesh whats up);

foreach (@array)
{
$x .= "$_ ";
}
print $x;
#now $x contains hi venkatesh whats up
 
G

Gunnar Hjalmarsson

How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2
instead to store hi venkatesh

$x = join ' ', %{ $array[0] };

If you rather mean that you have the array

@array = ("hi","venkatesh");

you can simply do

$x = "@array";

HTH
 
B

Ben Morrow

Quoth Gunnar Hjalmarsson said:
How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2
instead to store hi venkatesh

$x = join ' ', %{ $array[0] };

If you rather mean that you have the array

@array = ("hi","venkatesh");

you can simply do

$x = "@array";

....but be careful about $". Either of

{
local $" = ' ';
$x = "@array";
}

or

$x = join ' ', @array;

would be safer.

Ben
 
T

Tad J McClellan

Oliver Abram said:
Or maybe this one:


@array = qw (hi venkatesh whats up);

foreach (@array)
{
$x .= "$_ ";
}


That leaves an extra space character at the end of $x.

$x = join ' ', @array; # no trailing space
 
J

Jürgen Exner

Hi all,
How to assign array variable to a scalar retaining the content of the
array in the scalar variable not number of elements ;
eg:
@array={"hi","venkatesh"};
$x=@array;
I dont want $x to store 2

Well, it doesn't. $x contains 1 because the only element in @array is the
reference to the anonymous hash {'hi' => 'venkatesh'}
instead to store hi venkatesh

Do you mean the text 'hi venkatesh'?
Luckily Perl is smart enough to allow using join() on hashes, too. So you
can just join the key and value of the anonymous hash:

print join (' ', %{$array[0]});

Jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top