Re: any built in function to take out null arrays elements?
by CountZero (Bishop) on Sep 21, 2007 at 07:30 UTC
|
@userArray2 contains nothing, not even NULL, so there is nothing to delete.But perhaps you are thinking of an array containing empty elements: @array_with_empties = ('ONE', 'TWO', '', 'FOUR'); $array_with_empties[2]) is empty, but exists($array_with_empties[2]); will return a TRUE value! Indeed the array element exists, but is empty. To check if this array-element is empty, simply test it against the empty element '' or against undef. And to answer your question: you will always need a loop (explicit or implicit) to check for such elements.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
|
|
To check if this array-element is empty, simply test it against the empty element '' or against undef.
Empty, a blank string, and undef are different things though!
Update: string, not strong.
| [reply] |
|
|
They are indeed and zero will also have to be considered in such test, but failing more info of the practical side of what the OP wants, one cannot give more detailed help.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] |
Re: any built in function to take out null arrays elements?
by ikegami (Patriarch) on Sep 21, 2007 at 13:05 UTC
|
Since @userArray2 is empty, it doesn't add anything to @mainArray
my @userArray = qw(batman robin joker);
my @userArray2; #this is a null array
my @mainArray = (@userArray, @userArray2);
print("Number of elements: ", scalar(@mainArray), "\n"); # 3
If you want to filter out elements of an array, grep is usually the way to go.
my @mainArray = ('batman', 'robin', undef, 'joker');
print("Number of elements: ", scalar(@mainArray), "\n"); # 4
@mainArray = grep { defined } @mainArray;
print("Number of elements: ", scalar(@mainArray), "\n"); # 3
By the way, adding Array to your variable names is redundant. That's what @ means.
| [reply] [d/l] [select] |
Re: any built in function to take out null arrays elements?
by klekker (Pilgrim) on Sep 21, 2007 at 08:14 UTC
|
Try grep. At least you won't "see" a loop. ;)
k | [reply] |
Re: any built in function to take out null arrays elements?
by erroneousBollock (Curate) on Sep 21, 2007 at 07:16 UTC
|
my @userArray = qw(batman robin joker);
my @userArray2; #this is a null array
my @mainArray = (@userArray, @userArray2);
and then...
#@set should consist "batman robin joker", but not
#"batman robin joker NULL"
@mainArray should already contain what you want.
my @set = map { exists($_)? $_ : delete($_) } @mainArray;
I think you misunderstand exists and delete.
-David
| [reply] [d/l] [select] |
Re: any built in function to take out null arrays elements?
by papidave (Pilgrim) on Sep 21, 2007 at 19:58 UTC
|
As chromatic mentioned, the empty string '', the NULL value undef, and the empty list () are not the same thing. If you declare my @user2, you get an empty list, which is a valid list with zero elements. When you catenate lists, e.g.
my @main = ( @user1, @user2 );
you will have zero elements appended to the original 3, resulting in a list of 3 elements. There are no undef elements to compress. When I find one in my list, it probably means I have a defect in the code that built the list.
One thing to consider when looking for that defect is your use of delete on an array element. Doing so will create undefined elements in the list, not remove them. | [reply] [d/l] |
Re: any built in function to take out null arrays elements?
by girarde (Hermit) on Sep 21, 2007 at 21:07 UTC
|
If it's small enough to load all at once:
$array = join /$splitter/, @array; # where $splitter is a scalar that
+will not be in an element
$array =~ s/$splitter{2,}/$splitter/g;
@array = join $splitter, $array;
Variations would be required for multi-line strings, of course. Update: inserted a missing $. | [reply] [d/l] [select] |
Re: any built in function to take out null arrays elements?
by lyklev (Pilgrim) on Sep 22, 2007 at 21:40 UTC
|
You probably mean undefined elements, NULL is a c-thing.
One way to create an array with undefined elements is to use indexes and then skip a few. With grep -ing for defined elements, you can weed the undefined elements, as shown in the example below.
As mentioned, an empty array is not the same as an undefined array, and "0" is defined, so you don't want to skip it.
#!/usr/bin/perl
my (@ar1, @ar2);
$ar1[0] = 1;
$ar1[1] = 0; # a defined element, not NULL
$ar1[7] = 9; # skip, leaving 2..6 undefined
# dump array 1
print("\@ar1:\n");
for my $i (0..$#ar1) {
print(" $i : $ar1[$i]\n");
}
# weed the undefined elements
@ar2 = grep { defined $_ } @ar1;
print("\@ar2:\n");
for my $i (0..$#ar2) {
print(" $i : $ar2[$i]\n");
}
| [reply] [d/l] [select] |
|
|
@arr = grep(!/^$/, @arr);
| [reply] |
Re: any built in function to take out null arrays elements?
by tcf03 (Deacon) on Sep 22, 2007 at 11:26 UTC
|
#!/usr/local/bin/perl5.9.5
#
use feature ":5.10";
use strict;
use warnings;
print "content-type: text/html\n\n";
my @userArray = qw(batman robin joker);
my @userArray2; #this is a null array
my @mainArray = (@userArray, @userArray2);
my @set;
# A loop, just for this example...
for(@mainArray)
{
when ( ! defined $_ ) { next }
when ( /^$/ ) { next }
default { push @set, $_ }
}
say "@set";
I of course do not mean this as a serious (Use this in production) reply to the op.
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
--Ralph Waldo Emerson
| [reply] [d/l] |