 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 19th, 2009, 08:04 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to remove empty values in array
Dear all,
sample code:
========= @coins = ("Quarter","","Nickel");
how to delete the empty values from an array.
Thanks,
Thava
|
|

August 19th, 2009, 10:47 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
A really inefficient way to do it is simply to loop over the array and discard anything which is empty:
Code:
use strict;
use warnings;
my @array=("abc", "def", "", "ghij");
my @temp_array;
foreach(@array) {
if ($_) {
push(@temp_array, $_);
}
}
@array=@temp_array;
print join(", ", @array)."\n";
Yields:
Code:
C:\Temp>perl remove_elements.pl
abc, def, ghij
There's a better way to do it using a hash, but it escapes me at the moment....
|
|

August 20th, 2009, 04:44 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi Leigh,
thanks for your code and meanwhile i would like to know how to delete the particular element in an array by passing index.
Exemple:
======
@name ={"kevin","petter","ram","dhas","jenstin"}
i need like below code
============
delete(@name,2);
after the result should be:
@name ={"kevin","petter","dhas","jenstin"}
Thanks,
Thava
|
|

August 20th, 2009, 07:01 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by thava
hi Leigh,
thanks for your code and meanwhile i would like to know how to delete the particular element in an array by passing index.
Exemple:
======
@name ={"kevin","petter","ram","dhas","jenstin"}
i need like below code
============
delete(@name,2);
after the result should be:
@name ={"kevin","petter","dhas","jenstin"}
Thanks,
Thava
|
i got solution from google to achieve above i used below code its working fine...
delete @name[2];
thanks,
thava
|
|
 |