For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2006 > Re: counting number of empty strings in a multidimensional array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: counting number of empty strings in a multidimensional array
Mumia W.

2006-07-29, 6:58 pm

On 07/29/2006 10:03 AM, Jack wrote:
> Hi some folks helped me with uniques in this context, but this is a
> different beast..
> trying to count the number of empty strings in a multidimensional array
> column without creating too much memory overhead since I need to do
> this for each column (and I dont want to create a new array just for
> that column).. so I tried the below which is a similar approach to what
> some experts on this site suggested for uniques, but didnt lend itself
> to evaluating each value in the array as you will see in the code
> below, any ideas would be appreciated -
> Many thanks,
> Jack
>
> $nullcount= nulls(map { $_->[$p] } @multiarray);
> push @nullcounts, $count;
> $nullcount=0;
>
> sub nulls
> {
> my %nulls = ();
> if (/^\z/)) { $nulls{$1}++ foreach @_}
> return keys %nulls;
> }
>


This should count the number of empty strings in a column:

sub countNulls {
no warnings 'uninitialized';
my ($arref, $col) = @_;
my $count = 0;
$_->[$col] =~ /^\z/ && $count++ for (@$arref);
$count;
}

anno4000@radom.zrz.tu-berlin.de

2006-07-30, 3:58 am

Mumia W. <mumia.w.18.spam+nospam.usenet@earthlink.net> wrote in comp.lang.perl.misc:
> On 07/29/2006 10:03 AM, Jack wrote:


>
> This should count the number of empty strings in a column:
>
> sub countNulls {
> no warnings 'uninitialized';
> my ($arref, $col) = @_;
> my $count = 0;
> $_->[$col] =~ /^\z/ && $count++ for (@$arref);
> $count;
> }


The last three lines could be replaced by (untested)

scalar grep !length $_->[ $col], @$arref;

Anno
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com