Truncate an array when you have a ref to it?

Discussion in 'Perl Misc' started by A. Farber, Aug 27, 2008.

  1. A. Farber

    A. Farber Guest

    Hello,

    I have an object representing a player:

    sub new {
    my $package = shift;
    my $self = {
    .......
    CHAT => [[], [], []],
    };
    bless($self, $package);
    }

    and its CHAT member holds messages
    from 3 other players in an array of 3 arrays.

    Then I have a method which sends those
    messages over a socket to the player:

    $aref = $self->{CHAT}->[0];
    push @response, 'chat0=' . join '', @$aref;
    $self->{CHAT}->[0] = [];

    The last line truncates the sub-array
    (after the messages have been sent).

    I wonder if I can truncate that array by
    using the $aref ? Something like $#xxxx = -1;

    Thank you
    Alex
     
    A. Farber, Aug 27, 2008
    #1
    1. Advertising

  2. A. Farber wrote:
    > Hello,
    >
    > I have an object representing a player:
    >
    > sub new {
    > my $package = shift;
    > my $self = {
    > ......
    > CHAT => [[], [], []],
    > };
    > bless($self, $package);
    > }
    >
    > and its CHAT member holds messages
    > from 3 other players in an array of 3 arrays.
    >
    > Then I have a method which sends those
    > messages over a socket to the player:
    >
    > $aref = $self->{CHAT}->[0];
    > push @response, 'chat0=' . join '', @$aref;
    > $self->{CHAT}->[0] = [];
    >
    > The last line truncates the sub-array
    > (after the messages have been sent).
    >
    > I wonder if I can truncate that array by
    > using the $aref ? Something like $#xxxx = -1;


    Yes you could do that:

    $#$aref = -1;

    Or:

    @$aref = ();



    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, Aug 27, 2008
    #2
    1. Advertising

  3. A. Farber

    Ted Zlatanov Guest

    On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <> wrote:

    AF> $aref = $self->{CHAT}->[0];
    AF> push @response, 'chat0=' . join '', @$aref;
    AF> $self->{CHAT}->[0] = [];

    AF> The last line truncates the sub-array
    AF> (after the messages have been sent).

    Actually it assigns a new value to that reference. The old array still
    exists with the full contents if you have another reference to it (check
    $aref, for instance).

    AF> I wonder if I can truncate that array by
    AF> using the $aref ? Something like $#xxxx = -1;

    Sure:

    @$aref = ();

    or, equivalently,

    @{$self->{CHAT}->[0]} = ();

    Ted
     
    Ted Zlatanov, Aug 27, 2008
    #3
  4. A. Farber

    A. Farber Guest

    On Aug 27, 11:01 pm, Ted Zlatanov <> wrote:
    > On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <> wrote:
    >
    > AF>         $aref = $self->{CHAT}->[0];
    > AF>         push @response, 'chat0=' . join '', @$aref;
    > AF>         $self->{CHAT}->[0] = [];
    >
    > Actually it assigns a new value to that reference.  The old array still
    > exists with the full contents if you have another reference to it (check
    > $aref, for instance).
    >
    > AF> I wonder if I can truncate that array by
    > AF> using the $aref ? Something like $#xxxx = -1;
    >
    > @$aref = ();
    >
    > or, equivalently,
    >
    > @{$self->{CHAT}->[0]} = ();


    Oh, you're right. Thank you!
     
    A. Farber, Aug 28, 2008
    #4
    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. Replies:
    0
    Views:
    342
  2. Replies:
    22
    Views:
    764
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    352
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    145
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    621
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page