Help: About 'if' structure

Discussion in 'Perl Misc' started by Amy Lee, Jun 23, 2007.

  1. Amy Lee

    Amy Lee Guest

    Hello,

    I'm a Perl newbie, and I have a problem about the 'if' structure.

    If I wanna use more than one cpmparisons, how can I accomplish this
    function in perl?

    For example, in Shell script:

    if [ "$#" -eq 0 -a -e "$somefile" ]

    '-a' means "AND".

    And in perl, I have two comparison, I wanna combine them.

    1.if (@ARGV == 0)
    2.if (-e $somefile)

    Thank you in advance~

    Amy Lee
     
    Amy Lee, Jun 23, 2007
    #1
    1. Advertising

  2. Amy Lee <> wrote:


    > If I wanna use more than one cpmparisons, how can I accomplish this
    > function in perl?
    >
    > For example, in Shell script:


    > '-a' means "AND".



    In Perl, "and" means "AND", and "&&" means "AND" too.

    They are documented in the perlop.pod man page.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Jun 23, 2007
    #2
    1. Advertising

  3. Amy Lee

    Paul Lalli Guest

    On Jun 23, 7:12 am, "Amy Lee" <> wrote:
    > I'm a Perl newbie,


    Greetings.

    > and I have a problem about the 'if' structure.


    Not really. You have a question about logical operators. You just
    want to use them as the condition to an if statement.

    > If I wanna use more than one cpmparisons, how can I accomplish this
    > function in perl?
    >
    > For example, in Shell script:
    >
    > if [ "$#" -eq 0 -a -e "$somefile" ]
    >
    > '-a' means "AND".
    >
    > And in perl, I have two comparison, I wanna combine them.
    >
    > 1.if (@ARGV == 0)
    > 2.if (-e $somefile)


    There are two "AND" operators in Perl. '&&' and 'and'. They are
    functionally equivalent, but '&&' has a higher precedence than 'and'.
    So in your case, you might do:

    if (@ARGV == 0 and -e $somefile) { ... }
    or
    if (@ARGV == 0 && -e $somefile) { ... }

    For a full list of operators in Perl, and their precedence chart, open
    a command window and type:
    perldoc perlop

    Paul Lalli
     
    Paul Lalli, Jun 23, 2007
    #3
  4. Amy Lee

    Amy Lee Guest

    Thank you very much~

    I have solved my problem.
     
    Amy Lee, Jun 23, 2007
    #4
  5. Amy Lee

    Amy Lee Guest

    And Furthermore, if I wanna use "OR", how to accomplish it in perl?

    Amy
     
    Amy Lee, Jun 23, 2007
    #5
  6. Amy Lee <> wrote:

    > And Furthermore, if I wanna use "OR", how to accomplish it in perl?



    "or" or "||"

    You should check the documentation for the software you use *before*
    asking hundreds of people around the world to read it to you.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Jun 23, 2007
    #6
  7. Amy Lee

    Dr.Ruud Guest

    Amy Lee schreef:
    > And Furthermore, if I wanna use "OR", how to accomplish it in perl?


    Go and read perlop. Now.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jun 23, 2007
    #7
  8. Amy Lee

    Paul Lalli Guest

    On Jun 23, 9:37 am, "Amy Lee" <> wrote:
    > And Furthermore, if I wanna use "OR", how to accomplish it in perl?


    ..... did you think Tad and I pointed you at the `perldoc perlop`
    documentation for the heck of it?

    You've run out of free fish. Time to learn how to fish.

    Paul Lalli
     
    Paul Lalli, Jun 23, 2007
    #8
  9. On Sat, 23 Jun 2007 21:37:09 +0800, "Amy Lee"
    <> wrote:

    >And Furthermore, if I wanna use "OR", how to accomplish it in perl?


    Given that and and && were the solution to your "AND" question, it
    surprisingly turns out that or and || are those to the current one.
    Also surprisingly, this is clearly explained in the same document you
    have already been pointed to, which is

    perldoc perlop

    and which I encourage you to read in its entirety. It's an awesome
    reading, I guarantee!


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jun 23, 2007
    #9
  10. On Sat, 23 Jun 2007 14:33:30 -0000, Paul Lalli <>
    wrote:

    >You've run out of free fish. Time to learn how to fish.


    She'll tell you she's too hungry to lose her time playing games with
    lines and hooks! ;-)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jun 23, 2007
    #10
  11. Tad McClellan coughed up some electrons that declared:

    > Amy Lee <> wrote:
    >
    >
    >> If I wanna use more than one cpmparisons, how can I accomplish this
    >> function in perl?
    >>
    >> For example, in Shell script:

    >
    >> '-a' means "AND".

    >
    >
    > In Perl, "and" means "AND", and "&&" means "AND" too.
    >


    Although the operator precedence (priority) is different for

    "&&" vs "and"

    and

    "||" vs "or"

    Worth watching out for that, and not very obvious.

    To be fair, "perldoc" is a skill in itself - took me a while to discover its
    potential, back in the early days.

    To the OP:

    perldoc perl

    will list the "books" available to perldoc

    perldoc perlfunc

    is very useful

    perldoc -f somefunctionname

    is a quick lookup for the definition of a core function

    and

    perldoc Some::Module

    documents a module ("man Some::Module" often works too, at least on all
    Linux systems I've used)

    I think you would benefit from getting a couple of the O'Reilly books too -
    if you get only one, get the "Perl Cookbook" IMHO - instant recipes for all
    sorts of interesting problems, and a good demo of use of the languages in
    solving a variety of problems. Then again, I learn best by example, so you
    may like to consider "Learning Perl". It's a pretty good introduction.

    HTH

    Tim
     
    Tim Southerwood, Jun 23, 2007
    #11
  12. Amy Lee

    Paul Lalli Guest

    On Jun 23, 5:55 pm, Tim Southerwood <> wrote:

    > perldoc perl
    >
    > will list the "books" available to perldoc


    No it won't. It will give you an overview of the language, with
    select other documents available. `perldoc perltoc`, on the otherhand,
    will give you the Table of Contents of perldoc.

    Paul Lalli
     
    Paul Lalli, Jun 23, 2007
    #12
  13. Paul Lalli coughed up some electrons that declared:

    > On Jun 23, 5:55 pm, Tim Southerwood <> wrote:
    >
    >> perldoc perl
    >>
    >> will list the "books" available to perldoc

    >
    > No it won't. It will give you an overview of the language, with
    > select other documents available. `perldoc perltoc`, on the otherhand,
    > will give you the Table of Contents of perldoc.
    >
    > Paul Lalli


    You're right of course - I should have said "some of the books" - for some
    reason I tend to jump to perldoc perl for no apparently good reason, apart
    from (probably bad) habit.

    I'll get me coat...
     
    Tim Southerwood, Jun 23, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    760
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,231
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    613
  4. Replies:
    9
    Views:
    25,328
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    788
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page