Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > short form for (defined $x and defined $y and ... )?

Reply
Thread Tools

short form for (defined $x and defined $y and ... )?

 
 
Markus Dehmann
Guest
Posts: n/a
 
      03-25-2006
I often find myself typing

> unless(defined $x and defined $y and defined $z){ # ... many variables
> # ...
> }


Is there a shorter form to write that? (I mean apart from "&&" for "and").

Unfortunately,
> unless(defined ($x,$y,$z))

does not work.

Thanks!
Markus
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      03-25-2006
>>>>> "MD" == Markus Dehmann <> writes:

MD> I often find myself typing
>> unless(defined $x and defined $y and defined $z){ # ... many variables
>> # ...
>> }


then don't type that! i suspect a poor design that makes you check many
variables for defined. i have never seen that in quality code.

what that means is to solve the real problem (why are you doing that)
rather than the 'how do i do this?' problem.

post (a short complete program) that shows why you do all those defined
calls.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
robic0
Guest
Posts: n/a
 
      03-25-2006
On Sat, 25 Mar 2006 00:14:07 -0500, Markus Dehmann <> wrote:

>I often find myself typing
>
> > unless(defined $x and defined $y and defined $z){ # ... many variables
> > # ...
> > }

>
>Is there a shorter form to write that? (I mean apart from "&&" for "and").
>
>Unfortunately,
> > unless(defined ($x,$y,$z))

>does not work.
>
>Thanks!
>Markus


Stop thinking of Perl in a English sentence(y) way.
Even though the very "evil" word *unless* is available,
logic is Boolean! In more understandable terms its called
"Formal Symbolic Logic" and its offered as an elective at
every JC in the country (for 3 credits... whoop!).

Its a course without which, imo, you will run into problems
with the if/then/else construct. Btw, that course takes you
through the analysis of English sentences, then paragraphs,
then documents, where you have to write symbolic logic
expressions, turn English into logic, then for the grand
finally, you have to solve the logic equations just written
to give an overall TRUTH to the statement/paragraph/document.
Oh and there are the FORMAL ERRORS you have to deliniate.
20 or so. If you don't get an 'A' in this course there could
be trouble with programming.

"Shorter" is not better. Never was and never will be.
Shorthand notation you see on the usenet group are willfull
attempt to mislead. Deception is actually power here.

There is *NO* hidden logic, manifested by notation or keywords,
that make the code work faster or better beyond the fully
exposed if/then/else construct.

You actually have to "break the code down into simple logic"
before it can be optimized. If it can't be optimized when
expressed simply, then it *CANNOT* be optimized anyway else !!!

Hope you understand that?

robic0

 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      03-25-2006
Markus Dehmann wrote:
> I often find myself typing
>
> > unless(defined $x and defined $y and defined $z){ # ... many variables
> > # ...
> > }

>
> Is there a shorter form to write that? (I mean apart from "&&" for "and").
>
> Unfortunately,
> > unless(defined ($x,$y,$z))

> does not work.



Perhaps you have a preference between these two:

use List::MoreUtils qw/all/;

# ...

if (all { defined $_ } ($x, $y, $z))
{
print "Hello ";
}

# or

if (3 == grep defined $_, ($x, $y, $z))
{
print "World\n";
}

--
Hope this helps,
Steven

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      03-25-2006
On 24 Mar 2006 22:06:27 -0800, "" <> wrote:

>Markus Dehmann wrote:
>> I often find myself typing
>>
>> > unless(defined $x and defined $y and defined $z){ # ... many variables
>> > # ...
>> > }

>>
>> Is there a shorter form to write that? (I mean apart from "&&" for "and").
>>
>> Unfortunately,
>> > unless(defined ($x,$y,$z))

>> does not work.

>
>
>Perhaps you have a preference between these two:
>
>use List::MoreUtils qw/all/;
>
># ...
>
>if (all { defined $_ } ($x, $y, $z))
>{
> print "Hello ";
>}
>
># or
>
>if (3 == grep defined $_, ($x, $y, $z))
>{
> print "World\n";
>}


I've seen it all now, the whole thing. No need to wonder on
my death bed. An actual reference to a module that does the
&& for you. Thats pretty close to "lets all just take drugs
and get drunk". No need to program ever again.

Hey! Wheres my goddammed Perl program that does my programming?
Where'd I put that damned thing........ jeez!

robic0
 
Reply With Quote
 
Markus Dehmann
Guest
Posts: n/a
 
      03-25-2006
Uri Guttman wrote:
>>>>>>"MD" == Markus Dehmann <> writes:

>
>
> MD> I often find myself typing
> >> unless(defined $x and defined $y and defined $z){ # ... many variables
> >> # ...
> >> }

>
> then don't type that! i suspect a poor design that makes you check many
> variables for defined. i have never seen that in quality code.
>
> what that means is to solve the real problem (why are you doing that)
> rather than the 'how do i do this?' problem.
>
> post (a short complete program) that shows why you do all those defined
> calls.


my ($x,$y,$z) = $userInput =~ m/(.+?)\s\+\s(.+?)\s=\s(.+)/;
die("wrong input format") unless(defined $x and defined $y and defined $z);

I write lots of little scripts like that match user input like this.
Sure, it's not "quality code", but it's definitely not worth to spend
more code on the user input error checking for my purposes. So, if I
grep, say, 10 variables from the user input this (defined $x and defined
....) can be a little longish. I'd just like to say (defined ($x,$y,$z,...))

Okay, but you're right, maybe I could change the design. Maybe something
like this:

if( $userInput =~ m/(.+?)\s\+\s(.+?)\s=\s(.+)/ ){
($x,$y,$z) = ($1,$2,$2);
}
else{
die("wrong input format");
}

But again, this is longer, and I find my first version more intuitive.

Markus
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-25-2006
Markus Dehmann <> wrote in
news::

> if( $userInput =~ m/(.+?)\s\+\s(.+?)\s=\s(.+)/ ){
> ($x,$y,$z) = ($1,$2,$2);
> }
> else{
> die("wrong input format");
> }
>
> But again, this is longer, and I find my first version more intuitive.


$userInput =~ m/(.+?)\s\+\s(.+?)\s=\s(.+)/ or die "...";
my ($x, $y, $z) = ($1, $2, $3);

Sinan
--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-25-2006
<> wrote in comp.lang.perl.misc:
> Markus Dehmann wrote:
> > I often find myself typing
> >
> > > unless(defined $x and defined $y and defined $z){ # ... many variables
> > > # ...
> > > }

> >
> > Is there a shorter form to write that? (I mean apart from "&&" for "and").
> >
> > Unfortunately,
> > > unless(defined ($x,$y,$z))

> > does not work.


[...]

> if (3 == grep defined $_, ($x, $y, $z))
> {
> print "World\n";
> }


I'm not too happy with this solution. For one, it must be changed in
two places when the set ($x, $y, $z) changes.

if ( 0 == grep !defined, $x, $y, $z )

is easier to maintain, but it suffers from double negation. Disguising one
negation as "0 ==" helps a little.

Like all grep's it runs through the whole list though the result is clear
after the first undefined element is found. That is inefficient if lists
are long and undefined elements are frequently present. List::Util has
first() to offer, but in this particular case there is a snag. From the
documentation:

first BLOCK LIST
Similar to "grep" in that it evaluates BLOCK setting $_ to each
element of LIST in turn. "first" returns the first element where
the result from BLOCK is a true value. If BLOCK never returns true
or LIST was empty then "undef" is returned.

Now, if we write

first { !defined } $x, $y, $z;

that will return an undefined value, if one is found (of course). It
will also return an undefined value if none is found (BLOCK never returns
true). That is unfortunate and makes first() unusable for this purpose.

If the designer had decided to return empty in the exceptional case,

sub first (&;@) {
my $cond = shift;
$cond->() and return $_ for @_;
return;
}

one could say

0 == (() = first { !defined } $x, $y, $z);

This is somewhat cryptic, but it would be the way to go if efficiency
was critical.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      03-25-2006
Anno Siegel wrote:
> <> wrote in comp.lang.perl.misc:
>
> [...]
>
>
> > if (3 == grep defined $_, ($x, $y, $z))
> > {
> > print "World\n";
> > }

>
> I'm not too happy with this solution. For one, it must be changed in
> two places when the set ($x, $y, $z) changes.
>
> if ( 0 == grep !defined, $x, $y, $z )
>
> is easier to maintain, but it suffers from double negation. Disguising one
> negation as "0 ==" helps a little.
>
> Like all grep's it runs through the whole list though the result is clear
> after the first undefined element is found. That is inefficient if lists
> are long and undefined elements are frequently present. List::Util has
> first() to offer, but in this particular case there is a snag. From the
> documentation:
>
> first BLOCK LIST
> Similar to "grep" in that it evaluates BLOCK setting $_ to each
> element of LIST in turn. "first" returns the first element where
> the result from BLOCK is a true value. If BLOCK never returns true
> or LIST was empty then "undef" is returned.
>
> [ ... ]




Ah, makes perfect sense . I hadn't thought about the
aspect of efficiency. Perhaps then I'd use the firstidx
function from List::MoreUtils.

Taking a peek at the XS source code for firstidx:

http://search.cpan.org/src/VPARSEVAL...9/MoreUtils.xs

I see a "break" out of the "for" loop, so this need not evaluate
more arguments than necessary:


if ( (firstidx { !defined } ($x, $y, $z) ) < 0 )
{
# do something...
}

--
Cheers,
Steven

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-25-2006
<> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > <> wrote in

> comp.lang.perl.misc:
> >
> > [...]
> >
> >
> > > if (3 == grep defined $_, ($x, $y, $z))
> > > {
> > > print "World\n";
> > > }

> >
> > I'm not too happy with this solution. For one, it must be changed in
> > two places when the set ($x, $y, $z) changes.
> >
> > if ( 0 == grep !defined, $x, $y, $z )
> >
> > is easier to maintain, but it suffers from double negation. Disguising one
> > negation as "0 ==" helps a little.
> >
> > Like all grep's it runs through the whole list though the result is clear
> > after the first undefined element is found. That is inefficient if lists
> > are long and undefined elements are frequently present. List::Util has
> > first() to offer, but in this particular case there is a snag. From the
> > documentation:
> >
> > first BLOCK LIST
> > Similar to "grep" in that it evaluates BLOCK setting $_ to each
> > element of LIST in turn. "first" returns the first element where
> > the result from BLOCK is a true value. If BLOCK never returns true
> > or LIST was empty then "undef" is returned.
> >
> > [ ... ]

>
>
>
> Ah, makes perfect sense . I hadn't thought about the
> aspect of efficiency. Perhaps then I'd use the firstidx
> function from List::MoreUtils.


Oh, good. Must have missed that in List::MoreUtils.

[...]

> if ( (firstidx { !defined } ($x, $y, $z) ) < 0 )


Aha. That's it then.

Unlike Uri, occasionally I do want to verify that all members of a list
(or an array) are defined. I found the behavior of List::Util::first
less than satisfactory in that regard. Nice to know about firstidx.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
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
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments