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

Re: [PBML] Re: Reading a file

Expand Messages
  • J.E. Cripps
    ... One might change the length test thusly: #!/usr/bin/perl -w use strict; my $length = shift; while ( ) { chomp; print( $_ n ) if length == $length; } which
    Message 1 of 10 , Apr 6, 2006
      Chad Perrin <perrin@...> did lately instruct us with:

      > Fore!!!
      > #!/usr/bin/perl -w
      > use strict;
      > while (<>) { chomp; print("$_\n") if length == 8; }

      Chad: to run (having saved the code in a file "len8":

      > $ len8 words.txt

      One might change the length test thusly:

      #!/usr/bin/perl -w
      use strict;
      my $length = shift;
      while (<>) { chomp; print("$_\n") if length == $length; }

      which is run:

      lentest.pl 8

      or 7, 6, or the integer of your choice. For "shift" see
      perldoc perlfunc, or page 215 of the Camel Book (_Programming Perl_).

      > Chad: This version uses the diamond operator to take input when calling
      > the script.

      More formally designated, at least in the Camel Book as the "line input
      operator" and commonly also as the "angle brackets" or "angle operator"
      It's discussed on page 53 of the Camel.

      But -- after some looking I have failed to find a similar explication
      in perldoc perlop. This might be oversight on my part. The < >
      operator is discussed in numerous sites, e.g.

      Programming:Perl User I/O - Wikibooks

      > Chad: additional note: Because $_ is an implicit scalar variable here,
      > the chomp and length functions automatically default to using that
      > variable in this context. If you want to make your script someone more
      > readable to someone not familiar with Perl's quirks, of course, you could
      > use an explicit variable instead.

      Some discussion of the $_ :
      http://perldoc.perl.org/perlvar.html perlvar - perldoc.perl.org

      The < > and the implicit $_ are quintessial Perl idioms. The
      reason they are employed in most persons' first attempts is
      because they provide useful solutions to common tasks.
    Your message has been successfully submitted and would be delivered to recipients shortly.