For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Delete an element in an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Delete an element in an array
Oliver Fuchs

2005-02-21, 8:56 pm

Hi,

I want to save names from <STDIN> to an array.
Afterwards I want to delete a single name in the array received again from
<STDIN>.

This is what I have:

#!/usr/bin/perl

use warnings;

print "Some names please: \n";
@names = <STDIN>;
print "Delete one name? \n";
$deleted = <STDIN>;
foreach $item (@names) {
unless ($item eq $deleted){
push (@aonly, $item);
}
};
print "Your names:\n", @aonly, "\n";


My question is how can I delete an element of the array directly (something
like
pop (@names, $item);
).
Is there a chance to delete it without using the index?

Oliver
--
.... don't touch the bang bang fruit
Wagner, David --- Senior Programmer Analyst --- WG

2005-02-21, 8:56 pm

Oliver Fuchs wrote:
> Hi,
>=20
> I want to save names from <STDIN> to an array.
> Afterwards I want to delete a single name in the array received again
> from <STDIN>.
>=20
> This is what I have:
>=20
> #!/usr/bin/perl
>=20
> use warnings;
>=20
> print "Some names please: \n";
> @names =3D <STDIN>;
> print "Delete one name? \n";
> $deleted =3D <STDIN>;
> foreach $item (@names) {
> unless ($item eq $deleted){
> push (@aonly, $item);
> }
> };
> print "Your names:\n", @aonly, "\n";
>=20
>=20
> My question is how can I delete an element of the array directly
> (something like
> pop (@names, $item);
> ).
> Is there a chance to delete it without using the index?
>=20
> Oliver
> --
> ... don't touch the bang bang fruit


You would use splice to remove an item. But if I was going to do this, th=
en I would use a hash and then delete the item and not do the searching thr=
ough the array. If you only have 5 items, okay, but 10000 then you have to =
search at least 5000 entires ( on avg ) to get your item. But if name is th=
e key into the hash, then one action to delete the name.

Wags ;)


****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Bob Showalter

2005-02-22, 8:55 am

Oliver Fuchs wrote:
> Hi,
>
> I want to save names from <STDIN> to an array.
> Afterwards I want to delete a single name in the array received again
> from <STDIN>.
>
> This is what I have:
>
> #!/usr/bin/perl
>
> use warnings;


We always recommend:

use strict;

>
> print "Some names please: \n";
> @names = <STDIN>;
> print "Delete one name? \n";
> $deleted = <STDIN>;
> foreach $item (@names) {
> unless ($item eq $deleted){
> push (@aonly, $item);
> }
> };


This can be written as:

@aonly = grep $_ ne $item, @names;

or, if you just want one array:

@names = grep $_ ne $item, @names;

> print "Your names:\n", @aonly, "\n";
>
>
> My question is how can I delete an element of the array directly
> (something like
> pop (@names, $item);
> ).
> Is there a chance to delete it without using the index?


No; you have to know the index, or use a hash instead of an array.

>
> Oliver
> --
> ... don't touch the bang bang fruit

Oliver Fuchs

2005-02-26, 8:55 am

On Mon, 21 Feb 2005, Wagner, David --- Senior Programmer Analyst --- WGO wrote:

> Oliver Fuchs wrote:
>
> You would use splice to remove an item. But if I was going to do this, then I would use a hash and then delete the item and not do the searching through the array. If you only have 5 items, okay, but 10000 then you have to search at least 5000 entires

( on avg ) to get your item. But if name is the key into the hash, then one action to delete the name.
>
> Wags ;)
>


Hi,
thanks for answering. So I have to use a hash ... will try it.

Thanks
Oliver
--
.... don't touch the bang bang fruit
Oliver Fuchs

2005-02-26, 8:55 am

On Mon, 21 Feb 2005, Bob Showalter wrote:

> Oliver Fuchs wrote:
>
> We always recommend:
>
> use strict;
>
>
> This can be written as:
>
> @aonly = grep $_ ne $item, @names;
>
> or, if you just want one array:
>
> @names = grep $_ ne $item, @names;
>
>
> No; you have to know the index, or use a hash instead of an array.
>


Thanks for your help. So i will try to use a hash.

Oliver

--
.... don't touch the bang bang fruit
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com