RE: empty versus zero
Thanks Will but I'll own up to maybe not reading the spec closely enough. Is the string " " to be considered empty? It passes defined() nevertheless...as does the empty string ''. Embarrassing. > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of > Will of Thornhenge > Sent: Wednesday, August 13, 2003 8:36 PM > To: [EMAIL PROTECTED] > Cc: 'David Byrne' > Subject: Re: empty versus zero > > > I prefer Lynn. Rickards' method to the others I've seen mentioned. > Testing whether the value is defined will always work quietly and > swiftly; several of the other tests proposed will generate warnings > under some conditions, which can really bog down a loop. > > Lynn. Rickards wrote: > > > > > >>I think this is a fairly simple question... > >>How can I count empty values in an array? This count > >>should not include zeros or non-empty values. Below > >>is my current script, but it isn't working properly. > >> > >>I appreciate any assitance that you may provide. > >> > >>Thank you, > >>David > >> > >>#!perl -w > >># Count missing values > >>while (my $line = <>) { > >>chomp ($line); > >>my ($probe_id,$expression) = split /\t/, $line, 2; > >>my @expression = split /\t/, $expression; > >>my $count = 0; > >>foreach my $value (@expression) { > >># if (!$value) { > >> > > > > One way could be to use defined() > > > > unless(defined($value)) $count += 1; > > > >># } > >>} > >>print "$count\n"; > >>} > > Another way, utilizing the magic "for" and "defined" do with "$_": > > > for (@expression) { $count++ if not defined} > print $count, "\n"; > > > So lots of different ways to do it. > > > -- > Will Woodhull > [EMAIL PROTECTED] > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: empty versus zero
Lynn. Rickards wrote: > Thanks Will but I'll own up to maybe not reading the spec closely > enough. Is the string " " to be considered empty? It passes defined() > nevertheless...as does the empty string ''. Embarrassing. I think the string " " is not empty. It is just initialized to a string containing nothing. But it is different from undef, which means it is even not initialized. In my opinion, it is similar to the following C situation: 1. char *pstr = calloc((size_t)100, (size_t)1); // empty string 2. char *pstr // now pstr is undef. no memory is allocated, and no initialization is done. Regards, Xu Qiang ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: empty versus zero
Lynn. Rickards wrote: Thanks Will but I'll own up to maybe not reading the spec closely enough. Is the string " " to be considered empty? It passes defined() nevertheless...as does the empty string ''. Embarrassing. No, you were correct; the context that was supplied showed that the "empty" array elements had to be populated with nulls. The quick test code I wrote up gave me results that looked like what I've seen in sparse arrays, and I screwed up in not recognizing that split can't generate a sparse array. My apologies to the list. [Note to self: stop trying to interleave perl-win32-users interactions with other activities. Remember that the questions may be more subtle than they first appear.] -- Will Woodhull [EMAIL PROTECTED] -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Will of Thornhenge Sent: Wednesday, August 13, 2003 8:36 PM To: [EMAIL PROTECTED] Cc: 'David Byrne' Subject: Re: empty versus zero I prefer Lynn. Rickards' method to the others I've seen mentioned. Testing whether the value is defined will always work quietly and swiftly; several of the other tests proposed will generate warnings under some conditions, which can really bog down a loop. Lynn. Rickards wrote: I think this is a fairly simple question... How can I count empty values in an array? This count should not include zeros or non-empty values. Below is my current script, but it isn't working properly. I appreciate any assitance that you may provide. Thank you, David #!perl -w # Count missing values while (my $line = <>) { chomp ($line); my ($probe_id,$expression) = split /\t/, $line, 2; my @expression = split /\t/, $expression; my $count = 0; foreach my $value (@expression) { # if (!$value) { One way could be to use defined() unless(defined($value)) $count += 1; # } } print "$count\n"; } Another way, utilizing the magic "for" and "defined" do with "$_": for (@expression) { $count++ if not defined} print $count, "\n"; So lots of different ways to do it. -- Will Woodhull [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: empty versus zero
> I think this is a fairly simple question... > How can I count empty values in an array? This count > should not include zeros or non-empty values. Below > is my current script, but it isn't working properly. > > I appreciate any assitance that you may provide. > > Thank you, > David > > #!perl -w > # Count missing values > while (my $line = <>) { > chomp ($line); > my ($probe_id,$expression) = split /\t/, $line, 2; > my @expression = split /\t/, $expression; > my $count = 0; > foreach my $value (@expression) { ># if (!$value) { > One way could be to use defined() unless(defined($value)) $count += 1; ># } > } > print "$count\n"; > } > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: empty versus zero
> How can I count empty values in an array? This count > should not include zeros or non-empty values. Below > is my current script, but it isn't working properly. map { $count++ if(!length($_)) } @expression; ... will count zero-length elements. !c === Discreet Packaging: www.dronecolony.com C. Church === ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: empty versus zero
if (! defined ($value)) { print "1 value not defined\n";} if ($value eq "") { print "2 Found empty string ($value)\n"; } if ($value == 0) { print "3 Found a zero too ($value)\n"; } $value = ''; if (defined ($value)) { print "4 value now is defined but null ($value)\n";} if ($value eq "") { print "5 Found empty string ($value)\n"; } if ($value == 0) { print "6 Found a zero too ($value)\n"; } $value = 0; if ($value eq '') { print "7 Found empty string ($value)\n"; } if ($value == 0) { print "8 Found a zero ($value)\n"; } Because I had a similar problem a few days ago that I never did figure out, I've been following this and doing a little experimenting of my own, and in this code above all the print statements execute but 7. So, undefined it checks null or zero. Defined, and given a null value, it checks null or zero, Defined and given a zero value, it check only as zero. You'll also notice that in the case of the null values even though the if tests true, the attempt to print $value results in nothing between the ( )'s. Undefined can work, but it's not exactly highly maintainable. The variable could easily be accidentally defined in the code above as PERL automatically defines any variable that's used. I thought the "eq" and "==" operators might have something to do with it but in this particular case of a variable being automatically "set" (defined is not the right word:) to this zero/null on first use, either returns true. What I wonder is how does PERL go about pulling off this particular piece of "magic?" In C, the type checking would drive you nuts trying to make this happen. Length is the only thing I've seen so far that works reliably and that may only be because I haven't thought of the catch to that one yet. Good luck and one other comment, "simple" this is not :) ms At 03:52 PM 8/13/03, you wrote: By the nature if the array processing, the element ($value) will always turn true based on your current if statement. I think you want to change your if, to.. if ($value eq "") { print "Found empty string ($value)\n"; $count += 1; } -Original Message- From: David Byrne [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 13, 2003 8:55 AM To: [EMAIL PROTECTED] Subject: empty versus zero Dear experts, I think this is a fairly simple question... How can I count empty values in an array? This count should not include zeros or non-empty values. Below is my current script, but it isn't working properly. I appreciate any assitance that you may provide. Thank you, David #!perl -w # Count missing values while (my $line = <>) { chomp ($line); my ($probe_id,$expression) = split /\t/, $line, 2; my @expression = split /\t/, $expression; my $count = 0; foreach my $value (@expression) { if (!$value) { $count += 1; } } print "$count\n"; } __ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs