Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > problem with 'or' statement

Reply
Thread Tools

problem with 'or' statement

 
 
lucas
Guest
Posts: n/a
 
      01-21-2004
is there some way i can write:
if (some_routine()) { do_this(); and_that(); }

like so:
some_routine() || { do_this(); and_that(); }

the reason is, i have a bunch of code that would be a pain in the ass to mod
if i have to rewrite it as the former

thx
--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-21-2004
lucas wrote:
> is there some way i can write:
> if (some_routine()) { do_this(); and_that(); }
>
> like so:
> some_routine() || { do_this(); and_that(); }


This is valid code, equivalent to the first example:

some_routine() and do { do_this(); and_that() };

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-21-2004
lucas <(E-Mail Removed)> wrote:

> Subject: problem with 'or' statement



Did you mean to mention the "or" operator somewhere in your post?

If not, then why the misleading choice of Subject?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
$_@_.%_
Guest
Posts: n/a
 
      01-21-2004
> is there some way i can write:
> if (some_routine()) { do_this(); and_that(); }
>
> like so:
> some_routine() || { do_this(); and_that(); }
>
> the reason is, i have a bunch of code that would be a pain in the ass to mod
> if i have to rewrite it as the former


$var = &some_routine(); #make sure you use the return function in your subroutine.
if ($var == 1) { #do something or nothing }
else { #do something or nothing}

 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      01-21-2004
lucas wrote:
> is there some way i can write:
> if (some_routine()) { do_this(); and_that(); }
>
> like so:
> some_routine() || { do_this(); and_that(); }


Well, no. They don't do the same thing. The first does do_this()
and and_that() if some_routine() is true. The second does the
two functions only if some_routine() *isn't* true. Perhaps you
wanted this, instead:

some_routine() && { do_this(); and_that(); }

Chris Mattern

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-21-2004
$_@_.%_ <$_@_.%_> wrote:
>> is there some way i can write:
>> if (some_routine()) { do_this(); and_that(); }
>>
>> like so:
>> some_routine() || { do_this(); and_that(); }
>>
>> the reason is, i have a bunch of code that would be a pain in the ass to mod
>> if i have to rewrite it as the former

>
> $var = &some_routine();



Why are you using the ampersand?

$var = some_routine();


> #make sure you use the return function in your subroutine.



Why do you need to make sure you use the return function in your subroutine?


> if ($var == 1) { #do something or nothing }
> else { #do something or nothing}



Is your followup somehow related to the question that was asked?

I'm not seeing it...


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      01-21-2004
Thanks everyone for your posts


Gunner, Chris and Bernard have exactly what I need. I just never thought of
using the do {} to exectute the routines.

Tad: || = or

Thanks again
--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-21-2004
>>>>> "l" == lucas <(E-Mail Removed)> writes:

l> Thanks everyone for your posts
l> Gunner, Chris and Bernard have exactly what I need. I just never thought of
l> using the do {} to exectute the routines.

l> Tad: || = or

no it doesn't. you will burn yourself if you think that.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- 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
 
Tad McClellan
Guest
Posts: n/a
 
      01-21-2004
lucas <(E-Mail Removed)> wrote:

> Tad: || = or



No it doesn't.

You should perhaps go find out what is different between them:

perldoc perlop


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      01-22-2004
Michele Dondi wrote:

> On Wed, 21 Jan 2004 12:17:59 -0500, lucas <(E-Mail Removed)> wrote:
>
>>Tad: || = or

>
> (1) || != or (see perldoc perlop)
>
> $x = 0 or print 'cool!';
> $y = 0 || print 'cool!';
> print $x,$y;
>
> (2) I bet you whatever you like Tad knows that || is "much like" 'or'.
> He means that you didn't have a "problem with 'or' statement".
>
> (3) I decided not to be fussy in my previuous post in this thread, but
> your second snippet (notwithstanding the fact that it was incorrect -
> as you knew, and this is the reason why you were asking here),
> suggested a *working* form (e.g. that with 'do') that was not
> equivalent to the first snippet: you should have asked about the 'and'
> operator!
>

1:I actually forgot a ! in if (!some_routine()) {}
2:the reason I thought that || = or was because I've seen things like
open(FILE,"file") || die; and open(FILE,"file") or die;
3:thanks for clearing up the || != or thing for me

--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
 
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
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments