Re: How to delete an element in array?

2001-05-30 Thread Aaron Craig

At 11:12 29.05.2001 -0700, you wrote:

>Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
>probably coming back out when pseudo-hashes get removed in 5.10.
>("Death to pseudo-hashes!")

pseudo-hash?
Aaron Craig
Programming
iSoftitler.com




Re: How to delete an element in array?

2001-05-29 Thread Nic LAWRENCE

Ignore me if i'm talking poo, (especially since I didn't see the original
message),... but surely splice() would be best suited for working with an
ARRAY, but you all seem to be talking about hashes... sooo



- Original Message -
From: "Brett W. McCoy" <[EMAIL PROTECTED]>
To: "Walt Mankowski" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Tuesday, May 29, 2001 8:49 PM
Subject: Re: How to delete an element in array?


>
> On Tue, 29 May 2001, Walt Mankowski wrote:
>
> > On Tue, May 29, 2001 at 11:59:18AM -0700, Randal L. Schwartz wrote:
> > > >>>>> "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:
> > >
> > > >> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and
is
> > > >> probably coming back out when pseudo-hashes get removed in 5.10.
> > > >> ("Death to pseudo-hashes!")
> > >
> > > Brett> Argh.  I didn't realize that.  Is this specifically delete on
array
> > > Brett> elements?
> > >
> > > Yes.
> >
> > Is there any difference in 5.6.1 between
> >
> > delete $array[$index];
> >
> > and
> >
> > $array[$index] = undef;
> >
> > There doesn't seem to be from the man page.  If there's not, it
> > doesn't seem to be a particularly useful feature.
>
> I think the difference is what 'exists' returns.  Undefining an element
> still leaves the element in the array ('exists' would return true, but
> 'defined' would not).  This is the case with hashes, isn't it?
>
> -- Brett
>
> Brett W. McCoy
> Software Engineer
> Broadsoft, Inc.
> 240-364-5225
> [EMAIL PROTECTED]
>
>
>




Re: How to delete an element in array?

2001-05-29 Thread Brett W. McCoy

On Tue, 29 May 2001, Walt Mankowski wrote:

> On Tue, May 29, 2001 at 11:59:18AM -0700, Randal L. Schwartz wrote:
> > > "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:
> >
> > >> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
> > >> probably coming back out when pseudo-hashes get removed in 5.10.
> > >> ("Death to pseudo-hashes!")
> >
> > Brett> Argh.  I didn't realize that.  Is this specifically delete on array
> > Brett> elements?
> >
> > Yes.
>
> Is there any difference in 5.6.1 between
>
>   delete $array[$index];
>
> and
>
>   $array[$index] = undef;
>
> There doesn't seem to be from the man page.  If there's not, it
> doesn't seem to be a particularly useful feature.

I think the difference is what 'exists' returns.  Undefining an element
still leaves the element in the array ('exists' would return true, but
'defined' would not).  This is the case with hashes, isn't it?

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]




Re: How to delete an element in array?

2001-05-29 Thread Kevin Meltzer

On Tue, May 29, 2001 at 03:17:41PM -0400, Jeff Pinyan ([EMAIL PROTECTED]) spew-ed forth:
> On May 29, Walt Mankowski said:
> 
> >On Tue, May 29, 2001 at 11:59:18AM -0700, Randal L. Schwartz wrote:
> >> > "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:
> >> 
> >> >> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
> >> >> probably coming back out when pseudo-hashes get removed in 5.10.
> >> >> ("Death to pseudo-hashes!")
> >> 
> >> Brett> Argh.  I didn't realize that.  Is this specifically delete on array
> >> Brett> elements?
> >> 
> >> Yes.
> >
> >Is there any difference in 5.6.1 between
> >
> > delete $array[$index];
> >
> >and
> >
> > $array[$index] = undef;
> 
> Yes, when $index == $#array.
> 
>   @foo = ('a' .. 'z');
>   delete $foo[3];
>   print scalar @foo;  # 26
>   delete $foo[25];
>   print scalar @foo;  # 25

Also with exists()..

@array = qw(1 2 3 4 5);

delete $array[2];
print exists $array[2] ? "exists" : "doesn't exist\n";
$array[3] = undef;
print exists $array[3] ? "exists" : "doesn't exist\n";

Cheers,
Kevin

BTW.. I think this was introduced in 5.6.0, not 5.6.1. At least according to my
version of 5.6.0, and perldelta:

=head2 exists() and delete() are supported on array elements

-- 
[Writing CGI Applications with Perl - http://perlcgi-book.com]
"If you understand what you're doing, you're not learning anything."
-- Abraham Lincoln



Re: How to delete an element in array?

2001-05-29 Thread Randal L. Schwartz

> "Walt" == Walt Mankowski <[EMAIL PROTECTED]> writes:

Walt> Is there any difference in 5.6.1 between

Walt>   delete $array[$index];

Walt> and

Walt>   $array[$index] = undef;

Walt> There doesn't seem to be from the man page.  If there's not, it
Walt> doesn't seem to be a particularly useful feature.

Yes, but the difference is small, about the size of one SV, and
undetectable from Perl code (unless you count the introspective stuff
like Devel::Peek).

It's a microoptimization that helps fix things broken by what was
expected to be an optimization with pseudohashes.  Turns out that a
pseudohash is only 15% faster, but slows all hashes down by 15%, so
the code for pseudohashes penalizes everyone by 15% and only itself
gets back to a null savings over just using plain ordinary hashes!

So, pseudohashes must die, and we get our 15% back for the 99% of the
programs that didn't use pseudohashes. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: How to delete an element in array?

2001-05-29 Thread Jeff Pinyan

On May 29, Walt Mankowski said:

>On Tue, May 29, 2001 at 11:59:18AM -0700, Randal L. Schwartz wrote:
>> > "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:
>> 
>> >> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
>> >> probably coming back out when pseudo-hashes get removed in 5.10.
>> >> ("Death to pseudo-hashes!")
>> 
>> Brett> Argh.  I didn't realize that.  Is this specifically delete on array
>> Brett> elements?
>> 
>> Yes.
>
>Is there any difference in 5.6.1 between
>
>   delete $array[$index];
>
>and
>
>   $array[$index] = undef;

Yes, when $index == $#array.

  @foo = ('a' .. 'z');
  delete $foo[3];
  print scalar @foo;  # 26
  delete $foo[25];
  print scalar @foo;  # 25

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **




Re: How to delete an element in array?

2001-05-29 Thread Walt Mankowski

On Tue, May 29, 2001 at 11:59:18AM -0700, Randal L. Schwartz wrote:
> > "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:
> 
> >> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
> >> probably coming back out when pseudo-hashes get removed in 5.10.
> >> ("Death to pseudo-hashes!")
> 
> Brett> Argh.  I didn't realize that.  Is this specifically delete on array
> Brett> elements?
> 
> Yes.

Is there any difference in 5.6.1 between

delete $array[$index];

and

$array[$index] = undef;

There doesn't seem to be from the man page.  If there's not, it
doesn't seem to be a particularly useful feature.

Walt

-- 
Walter C. Mankowski
Senior Software EngineerMyxa Corporation
phone: (610) 234-2626   fax: (610) 234-2640
email: [EMAIL PROTECTED]http://www.myxa.com




Re: How to delete an element in array?

2001-05-29 Thread Randal L. Schwartz

> "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:

>> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
>> probably coming back out when pseudo-hashes get removed in 5.10.
>> ("Death to pseudo-hashes!")

Brett> Argh.  I didn't realize that.  Is this specifically delete on array
Brett> elements?

Yes.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: How to delete an element in array?

2001-05-29 Thread Brett W. McCoy

On 29 May 2001, Randal L. Schwartz wrote:

> Brett> Same thing.  See 'perldoc -f delete'.  It can be used on an
> Brett> array, array slice, hash or hash slice.  Note that for an
> Brett> array, you will still need to 'shift' down all of the
> Brett> subsequent elements since delete does not automatically shift
> Brett> down the indices. See the documentation on the 'splice' and
> Brett> 'shift' functions.  You may also want to see what 'pop' can do
> Brett> for you -- it pops off the end of the array (opposite of push),
> Brett> like for stack.
>
> Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
> probably coming back out when pseudo-hashes get removed in 5.10.
> ("Death to pseudo-hashes!")

Argh.  I didn't realize that.  Is this specifically delete on array
elements?

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]




Re: How to delete an element in array?

2001-05-29 Thread Randal L. Schwartz

> "Brett" == Brett W McCoy <[EMAIL PROTECTED]> writes:

Brett> On 29 May 2001, Jie Meng wrote:
>> I am a newbie. Would you please tell me how to delete an element in an Array?
>> 
>> We know we can use "delelte" to remove a hash element. How about array?

Brett> Same thing.  See 'perldoc -f delete'.  It can be used on an
Brett> array, array slice, hash or hash slice.  Note that for an
Brett> array, you will still need to 'shift' down all of the
Brett> subsequent elements since delete does not automatically shift
Brett> down the indices. See the documentation on the 'splice' and
Brett> 'shift' functions.  You may also want to see what 'pop' can do
Brett> for you -- it pops off the end of the array (opposite of push),
Brett> like for stack.

Uh, careful.  This got added to 5.6.1 to support pseudo-hashes and is
probably coming back out when pseudo-hashes get removed in 5.10.
("Death to pseudo-hashes!")

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: How to delete an element in array?

2001-05-29 Thread Brett W. McCoy

On 29 May 2001, Jie Meng wrote:

> I am a newbie. Would you please tell me how to delete an element in an Array?
>
> We know we can use "delelte" to remove a hash element. How about array?

Same thing.  See 'perldoc -f delete'.  It can be used on an array, array
slice, hash or hash slice.  Note that for an array, you will still need to
'shift' down all of the subsequent elements since delete does not
automatically shift down the indices. See the documentation on the
'splice' and 'shift' functions.  You may also want to see what 'pop' can
do for you -- it pops off the end of the array (opposite of push), like
for stack.

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]




Re: How to delete an element in array?

2001-05-29 Thread Jeff Pinyan

On May 29, Jie Meng said:

>I am a newbie. Would you please tell me how to delete an element in an
>Array?

The delete() function works on arrays (in Perl 5.6), but it doesn't remove
the element (unless it occurs at the end of the array).

You want to use the splice() function.

  splice @array, $index, 1;  # removes the $index'th element

Please read 'perldoc -f splice' at your command-line, or go to
http://www.perldoc.org/.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **




How to delete an element in array?

2001-05-29 Thread Jie Meng

Hi,

I am a newbie. Would you please tell me how to delete an element in an Array?

We know we can use "delelte" to remove a hash element. How about array?

Thanks,

Jie Meng


Get free email and a permanent address at http://www.amexmail.com/?A=1