Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 19th, 2009, 09:04 AM
Authorized User
Points: 181, Level: 3
Points: 181, Level: 3 Points: 181, Level: 3 Points: 181, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old August 19th, 2009, 11: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....
Reply With Quote
  #3 (permalink)  
Old August 20th, 2009, 05:44 AM
Authorized User
Points: 181, Level: 3
Points: 181, Level: 3 Points: 181, Level: 3 Points: 181, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #4 (permalink)  
Old August 20th, 2009, 08:01 AM
Authorized User
Points: 181, Level: 3
Points: 181, Level: 3 Points: 181, Level: 3 Points: 181, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04: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



All times are GMT -4. The time now is 02:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.