p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   how to remove empty values in array (http://p2p.wrox.com/showthread.php?t=75790)

thava August 19th, 2009 08:04 AM

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

Leigh August 19th, 2009 10:47 PM

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....

thava August 20th, 2009 04:44 AM

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

thava August 20th, 2009 07:01 AM

Quote:

Originally Posted by thava (Post 245578)
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


All times are GMT -4. The time now is 05:48 PM.

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