Single questions about file processing in perl

Discussion in 'Perl Misc' started by dima@inotech.ru, Jun 29, 2005.

  1. Guest

    Hello all!

    The following code is correct :

    open(FD,"< my.txt") or die("Error");
    while(<FD>)
    {
    print "$_\n";
    exit(0) if ($_eq "STOP");
    }

    But this code work incorrect :

    open(FD,"< my.txt") or die("Error");
    $_="NOT_STOP";
    while(<FD> and ($_ne "STOP") )
    {
    print "$_\n";
    }
    print "$_\n";
    exit(0);
    Both example are equivalent? Or I wrong?

    Furthermore, why this code is incorrect :
    while(<FD> and 1)
    {
    print "$_\n";
    }
    Thanks!
     
    , Jun 29, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > Hello all!
    >
    > The following code is correct :
    >
    > open(FD,"< my.txt") or die("Error");
    > while(<FD>)
    > {
    > print "$_\n";
    > exit(0) if ($_eq "STOP");
    > }
    >
    > But this code work incorrect :


    This is a poor error description. You should always tell us HOW it is
    incorrect. What did it not do that you expected it to do?
    Fortunately, in this case, the answer is plain.

    > open(FD,"< my.txt") or die("Error");
    > $_="NOT_STOP";
    > while(<FD> and ($_ne "STOP") )
    > {
    > print "$_\n";
    > }
    > print "$_\n";
    > exit(0);
    > Both example are equivalent? Or I wrong?


    Unfortunately, they are not equivalent. The construct of
    while (<FD>){ ... }
    is "magical". When <> is the only operation or statement inside the
    while() condition, then, AND ONLY THEN, does $_ get assigned the return
    value of the <> operation. In ANY other circumstance, you need to
    explicitly assign $_.

    while (defined($_ = <FD>) and ($_ ne "STOP")){ ... }

    > Furthermore, why this code is incorrect :
    > while(<FD> and 1)
    > {
    > print "$_\n";
    > }


    Same reason as above. The auto-assigning of $_ = <FD> occurs if and
    only if <FD> is the ONLY thing inside the while() condition. putting
    the additional code ("and 1") in the parentheses prevents this magic.

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Jun 29, 2005
    #2
    1. Advertising

  3. Joe Smith Guest

    wrote:

    > The following code is correct :
    >
    > open(FD,"< my.txt") or die("Error");
    > while(<FD>)
    > {
    > print "$_\n";
    > exit(0) if ($_eq "STOP");
    > }


    No, that is not correct. Since you neglected to chomp(), it prints
    out the contents of the file double spaced and does not stop at a
    line with "STOP" (unless you have a premature end-of-file).
    -Joe
     
    Joe Smith, Jul 17, 2005
    #3
    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. Randall Parker
    Replies:
    1
    Views:
    624
    S. Justin Gengo
    Dec 12, 2005
  2. Chris
    Replies:
    1
    Views:
    13,749
    Oisin
    Mar 24, 2006
  3. chris brat
    Replies:
    1
    Views:
    667
    chris brat
    May 10, 2006
  4. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    540
    Michael Foord
    Sep 17, 2004
  5. Replies:
    3
    Views:
    210
    Josef Moellers
    Nov 1, 2007
Loading...

Share This Page