Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
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
 
Old August 19th, 2009, 08:04 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old August 19th, 2009, 10:47 PM
Registered User
 
Join Date: Aug 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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....
 
Old August 20th, 2009, 04:44 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 20th, 2009, 07:01 AM
Authorized User
 
Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by thava View Post
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
remove empty elements but not ones with attributes dupdup XSLT 1 March 3rd, 2007 07:02 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
How to check if you have an empty array Ciarano VB How-To 9 March 31st, 2004 09:17 AM
Remove item from an array? jefferis PHP How-To 5 November 27th, 2003 11:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.