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

Re: [PBML] Re: About the angle-brackets

Expand Messages
  • M. Lewis
    ... Sreeram, Damien s comments are right on. I m not sure where you are in your learning process. If you re trying to learn to open files, then this comment
    Message 1 of 3 , Jul 6 11:31 AM
    • 0 Attachment
      Damien Carbery wrote:
      > --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
      > wrote:
      >
      >>Hi friends,
      >> I have one query about getting the input from the angle brackets
      >
      > ie from a file-handler within the angle brackets.
      >
      >> My code is like this:
      >>------------------------------------------------------
      >>#!/usr/bin/perl
      >>use strict;
      >>my $line;
      >>my $count = 0;
      >>my $IN;
      >>open(IN,"file") or die "Could not open file: $!\n";
      >>foreach $line (<IN>) {
      >> chomp($line);
      >> print("Line $count: $line\n");
      >> $count++;
      >> chomp($line = <IN> ); ## I expected the second line of "file" to
      >
      > be $line now. But it is not.
      >
      >> print("Line $count: $line\n");
      >> $count++;
      >> chomp($line = <IN> );
      >> print("Line $count: $line\n");
      >> $count++;
      >>}
      >>close IN;
      >>----------------------------------------------------------
      >>
      >>Contents of the file, "file" are as follows:
      >>
      >>Hello world
      >>How are you
      >>This is the third line
      >>We need to see if this works fine.
      >>------------------------------------------------------------
      >>
      >> My question is that, dont we get the data whenever we use the
      >
      > angle brackets? Do we get the data only at the beginning of the loop?
      >
      >>
      >>
      >>The output of the program is:
      >>
      >>Line 0: Hello world
      >>Line 1:
      >>Line 2:
      >>Line 3: How are you
      >>Line 4:
      >>Line 5:
      >>Line 6: This is the third line
      >>Line 7:
      >>Line 8:
      >>Line 9: We need to see if this works fine.
      >>Line 10:
      >>Line 11:
      >>--------------------------------------------------------------------
      >>
      >> I expected the 4 output lines in line0,1,2,3 but it is displaying
      >
      > in line 0,3,6,9.
      >
      >>
      >>In order to get this at each usage of the angle brackets, should we
      >
      > set any variable to any predefined value?
      >
      >>
      >>Kindly suggest.
      >>
      >>Thanks,
      >>Sreeram
      >>
      >
      >
      > The results are correct. You could have seen warning messages if you
      > turned warnings on by appending '-w' to the '#!/usr/bin/perl' line or
      > by adding 'use warnings;' after 'use strict;'.
      > Also, you don't need the '$IN' variable. The 'IN' used in 'open' has
      > nothing to do with '$IN'.
      >
      > The foreach loop causes the entire file to be read in ('<IN>' is
      > interpreted as being in array context so the file is read into an
      > anonymous array) and so there is nothing to read in your two further
      > calls to '<IN>' inside the loop.
      >
      > This version of the script will do the same thing and it uses the '$.'
      > built-in variable that tells you the line number of the file being
      > read.
      >
      > #!/usr/bin/perl -w
      > use strict;
      > open(IN,"file") or die "Could not open file: $!\n";
      > while (<IN>) {
      > chomp;
      > print("Line $.: $_\n");
      > }
      > close IN;
      >
      >
      >
      >

      Sreeram,

      Damien's comments are right on. I'm not sure where you are in your
      learning process. If you're trying to learn to open files, then this
      comment will be of no help to you in that regard.

      When I'm testing something, I like to simplify things as much as
      possible. You can make the contents of you "file" a part of your perl
      script. Then you don't have to open the file, close it, etc. Makes
      things a little bit simpler to understand.

      Here is Damien's code with the contents of your "file" included. Hope
      this helps.

      M


      #!/usr/bin/perl -w

      use strict;

      while (<DATA>){
      chomp;
      print("Line $.: $_\n");
      }

      __DATA__
      Hello world
      How are you
      This is the third line
      We need to see if this works fine.


      --

      Microwave: Signal from a friendly micro...
      13:20:01 up 7 days, 51 min, 7 users, load average: 0.32, 0.29, 0.15

      Linux Registered User #241685 http://counter.li.org
    Your message has been successfully submitted and would be delivered to recipients shortly.