Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Truncate an array when you have a ref to it?

Reply
Thread Tools

Truncate an array when you have a ref to it?

 
 
A. Farber
Guest
Posts: n/a
 
      08-27-2008
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



 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      08-27-2008
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
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-27-2008
On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <(E-Mail Removed)> 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
 
Reply With Quote
 
A. Farber
Guest
Posts: n/a
 
      08-28-2008
On Aug 27, 11:01*pm, Ted Zlatanov <(E-Mail Removed)> wrote:
> On Wed, 27 Aug 2008 13:34:00 -0700 (PDT) "A. Farber" <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 22 04-30-2008 10:42 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 6 04-29-2008 08:09 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 0 04-24-2008 08:23 PM
strong ref from weak ref? Navindra Umanee Ruby 2 02-12-2005 05:06 PM



Advertisments