Click here to get back home

assigning array to a scalar variable

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
assigning array to a scalar variable venkatesh.naughty 03-14-2008
Posted by Gunnar Hjalmarsson on March 14, 2008, 4:59 am
Please log in for more thread options
venkatesh.naughty@gmail.com wrote:
> How to assign array variable to a scalar retaining the content of the
> array in the scalar variable not number of elements ;
> eg:
> @array=;
> $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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Ben Morrow on March 14, 2008, 7:38 am
Please log in for more thread options

> venkatesh.naughty@gmail.com wrote:
> > How to assign array variable to a scalar retaining the content of the
> > array in the scalar variable not number of elements ;
> > eg:
> > @array=;
> > $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


Posted by Jürgen Exner on March 14, 2008, 9:16 am
Please log in for more thread options
venkatesh.naughty@gmail.com wrote:
>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=;
>$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 (' ', %);

Jue


Similar ThreadsPosted
Slice returned hash or access to its elements without assigning to a variable June 11, 2005, 10:35 am
Assigning pattern matches to an array December 30, 2006, 7:58 am
Can not add a scalar variable of path into @INC? May 8, 2007, 10:52 pm
how to "spell out" the elements in a scalar variable? May 23, 2005, 2:10 pm
understading double scalar variable $$ July 17, 2008, 7:55 pm
Hash keys to scalar variable conversion. May 18, 2006, 4:41 pm
How to extract all link from web page (loaded in variable $content) to scalar @links? September 11, 2006, 4:47 am
scalar to array? January 29, 2008, 1:50 pm
Using JOIN: Converting scalar to array August 23, 2005, 7:18 pm
How to pass an array and scalar as arguments to a subroutine/ June 28, 2006, 12:16 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap