Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ignore undef elements for array count

Reply
Thread Tools

Ignore undef elements for array count

 
 
David
Guest
Posts: n/a
 
      11-11-2004
Dear all,

How does one get the number of non-empty elements in an array? The
following snippet prints '3', I only want to count the non-empty
elements.

Thanks!

#!/usr/bin/perl

use strict;

local $\ = "\n";
my @arr = qw ( sissors paper undef );
print scalar @arr;
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-11-2004
David wrote:
> How does one get the number of non-empty elements in an array? The
> following snippet prints '3', I only want to count the non-empty
> elements.
>
> local $\ = "\n";
> my @arr = qw ( sissors paper undef );
> print scalar @arr;


But this array does not contain any undefined elements. The first element
contains the text 'sissors', the second the text 'paper', and the third the
text 'undef', all three of them well defined strings.

If you are talking about actual undefined values then just filter for
defined values before counting them:

print scalar grep defined, @arr;

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a count of unique elements in an array Dan Kohn Ruby 6 12-28-2013 11:02 AM
undef($foo) versus $foo = undef()? Tim McDaniel Perl Misc 6 08-19-2009 08:31 AM
Ignore + TEST + Ignore SpooderStank Computer Support 2 04-08-2004 11:26 AM
Searching for Exact Phrase - should I ignore the ignore words? Rob Meade ASP General 6 03-01-2004 11:28 AM
Using undef as an array subscript Yehuda Berlinger Perl Misc 8 07-01-2003 07:04 PM



Advertisments