Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Truncate an array when you have a ref to it? (http://www.velocityreviews.com/forums/t908210-truncate-an-array-when-you-have-a-ref-to-it.html)

A. Farber 08-27-2008 08:34 PM

Truncate an array when you have a ref to it?
 
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




John W. Krahn 08-27-2008 08:44 PM

Re: Truncate an array when you have a ref to it?
 
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

Ted Zlatanov 08-27-2008 09:01 PM

Re: Truncate an array when you have a ref to it?
 
On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <Alexander.Farber@gmail.com> 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

A. Farber 08-28-2008 06:11 AM

Re: Truncate an array when you have a ref to it?
 
On Aug 27, 11:01*pm, Ted Zlatanov <t...@lifelogs.com> wrote:
> On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <Alexander.Far...@gmail.com> 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!


All times are GMT. The time now is 09:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.