assigning array to a scalar variable

Discussion in 'Perl Misc' started by venkatesh.naughty@gmail.com, Mar 14, 2008.

  1. Guest

    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
    , Mar 14, 2008
    #1
    1. Advertising

  2. Peter Makholm, Mar 14, 2008
    #2
    1. Advertising

  3. 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={"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
    --
    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
    John W. Krahn, Mar 14, 2008
    #3
  4. Oliver Abram Guest

    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={"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
    Oliver Abram, Mar 14, 2008
    #4
  5. 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={"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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 14, 2008
    #5
  6. Ben Morrow Guest

    Quoth Gunnar Hjalmarsson <>:
    > 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={"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
    Ben Morrow, Mar 14, 2008
    #6
  7. Oliver Abram <> wrote:
    > 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={"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 .= "$_ ";
    > }



    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.noitatibaher\100cmdat/"
    Tad J McClellan, Mar 14, 2008
    #7
  8. 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={"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
    Jürgen Exner, Mar 14, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr_guy99493
    Replies:
    0
    Views:
    332
    mr_guy99493
    Feb 4, 2011
  2. Clint Olsen
    Replies:
    6
    Views:
    351
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Alex
    Replies:
    3
    Views:
    471
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    161
    Arndt Jonasson
    Jan 27, 2005
  5. weston
    Replies:
    1
    Views:
    246
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page