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 venkatesh.naughty on March 14, 2008, 4:39 am
Please log in for more thread options
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
instead to store hi venkatesh

Posted by Peter Makholm on March 14, 2008, 4:44 am
Please log in for more thread options
venkatesh.naughty@gmail.com writes:

> @array=;
> $x=@array;

I'm not sure this does what you believe it does. In any case you want
to read the perlreftut document:

http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlreftut.pod

//Makholm

Posted by John W. Krahn on March 14, 2008, 4:46 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
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Oliver Abram on March 14, 2008, 4:48 am
Please log in for more thread options
venkatesh.naughty@gmail.com schrieb:
> 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
> 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



Posted by Tad J McClellan on March 14, 2008, 7:40 am
Please log in for more thread options
> venkatesh.naughty@gmail.com schrieb:
>> 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
>> instead to store hi venkatesh
>
>
> 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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

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