Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Problem in puuting code in single line with foreach

Expand Messages
  • merlyn@stonehenge.com
    ... Ashish Hi, Ashish I am writing a perl program, which accepts arbitrary number of integers as input and prints all the even numbers. But I am not
    Message 1 of 3 , Jul 2, 2006
    • 0 Attachment
      >>>>> "Ashish" == Ashish Gupta <ashish_gupta_cal@...> writes:

      Ashish> Hi,
      Ashish> I am writing a perl program, which accepts arbitrary number of integers as input and prints all the even numbers. But I am not supposed to use any user-defined variable.

      Ashish> So following code works...

      Ashish> #! /usr/bin/perl -wl
      Ashish> foreach (@ARGV){
      Ashish> print unless($_ %2);
      Ashish> }

      Ashish> But problem is if I try to shrink the code, it doesn't works...
      Ashish> #! /usr/bin/perl -wl
      Ashish> print unless($_ %2) foreach (@ARGV);

      Ashish> I get the error...
      Ashish> syntax error at test5.pl line 3, near ") foreach "

      Ashish> Can someone please point out what's wrong in the above code.

      As you see, you can't stack them. The syntax for STATEMENT includes

      EXPR;
      EXPR foreach EXPR;
      EXPR unless EXPR;
      foreach (EXPR) BLOCK
      unless (EXPR) BLOCK

      Notice you can't put a STATEMENT where an EXPR is wanted. However,
      EXPR includes:

      do BLOCK

      where BLOCK is

      { STATEMENTS }

      and STATEMENTS is

      STATEMENT
      STATEMENTS STATEMENT

      So you could stack them with

      do { something unless something } foreach somethong;

      But please don't. The reason they don't stack is that it makes it a lot
      harder to understand.

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    • avnit singh
      hi , this is amazing you are messing up my friend see the syntax what will be printented if $_ is a wide space up front so you will get an error try for
      Message 2 of 3 , Jul 6, 2006
      • 0 Attachment
        hi ,
        this is amazing you are messing up my friend see the syntax what will
        be printented if $_ is a wide space up front so you will get an error
        try
        for each(@ARGV)
        {print ...}
        is corr
        --- In perl-beginner@yahoogroups.com, Ashish Gupta
        <ashish_gupta_cal@...> wrote:
        >
        > Hi,
        >
        > I am writing a perl program, which accepts arbitrary number of
        integers as input and prints all the even numbers. But I am not
        supposed to use any user-defined variable.
        >
        > So following code works...
        >
        > #! /usr/bin/perl -wl
        > foreach (@ARGV){
        > print unless($_ %2);
        > }
        >
        > But problem is if I try to shrink the code, it doesn't works...
        > #! /usr/bin/perl -wl
        > print unless($_ %2) foreach (@ARGV);
        >
        > I get the error...
        > syntax error at test5.pl line 3, near ") foreach "
        >
        > Can someone please point out what's wrong in the above code.
        >
        > Thanks and Regards,
        > Ashish
        >
        >
        > ---------------------------------
        > Sneak preview the all-new Yahoo.com. It's not radically different.
        Just radically better.
        >
        > [Non-text portions of this message have been removed]
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.