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

Re: Reading a file

Expand Messages
  • Robin
    Thanks for the help :-)
    Message 1 of 10 , Apr 5, 2006
      Thanks for the help :-)

      --- In perl-beginner@yahoogroups.com, "a_z0_9_blah" <a_z0_9_blah@...>
      wrote:
      >
      > --- In perl-beginner@yahoogroups.com, "Robin" <robin@> wrote:
      > >
      > > I'm going to apologize now for me being very, very new to perl.
      > I've
      > > just started learning about arrays and reading files. It's better
      > for
      > > me to talk to someone to get the full understanding...lol...me
      > talking
      > > to myself isn't working. I started a script that will read in a
      > text
      > > file that has 1275 lines. It's a wordlist. On each line the word is
      > > between 6 to 9 characters. Can someone point me in the right
      > direction
      > > for writing the script to be able to count the letters in each
      > word,
      > > looking for the words that contain 8 letters only and printing
      > them to
      > > screen or file. I've changed the script about 20 times and still
      > > cannot get it. Below is the script that I started.
      > >
      > > #!/usr/bin/perl -w
      > >
      > > open(MYFILE, "example.txt") || die "Can't open myfile: $!";
      > >
      > > @line=<MYFILE>;
      > >
      > > close(MYFILE);
      > >
      > > $a=@line;
      > >
      > > print "$a lines in this file.\n";
      > >
      > > Any help will be appreciated. I know the top part works because it
      > > tells me how many lines are in the file. Thanks again for any help.
      > >
      >
      > A couple of suggestions :-)
      >
      > At the top of your program, (after the #!... line),
      > use strict;
      > use warnings;
      >
      > (the -w switch you used is better replaced by the use warnings
      > pragma instead). strict will catch errors in your code. For a small
      > program like yours, that may not be obvious, but with a longer
      > program, use strict really pays off. It requires that you declare
      > your variables with my (my @line; my $a;)
      >
      > open(MYFILE, "example.txt") || die "Can't open myfile: $!";
      > chomp(my @line = <MYFILE>);
      > for my $word(@line) {
      > print if length $word == 8;
      > }
      >
      > Last, you can read lines one at a time instead of reading all of
      > them into an array as you did - saves memory and is less wasteful of
      > resourses.
      >
      > open(MYFILE, "example.txt") || die "Can't open myfile: $!";
      >
      > while (my $word = <MYFILE>) {
      > chomp $word; # remove the newline
      > print if length $word == 8;
      > }
      >
      >
      > Chris
      >
    • Chad Perrin
      ... use warnings; seems to be the new Right Way To Do It, but I ve yet to see an explanation for why it s preferable to -w. What are the reasons for
      Message 2 of 10 , Apr 5, 2006
        On Wed, Apr 05, 2006 at 07:38:45PM -0000, a_z0_9_blah wrote:
        >
        > A couple of suggestions :-)
        >
        > At the top of your program, (after the #!... line),
        > use strict;
        > use warnings;
        >
        > (the -w switch you used is better replaced by the use warnings
        > pragma instead).

        "use warnings;" seems to be the new Right Way To Do It, but I've yet to
        see an explanation for why it's preferable to -w. What are the reasons
        for preferring "use warnings;" over -w?

        --
        Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
        "A script is what you give the actors. A program
        is what you give the audience." - Larry Wall
      • Chad Perrin
        ... Fore!!! #!/usr/bin/perl -w use strict; while ( ) { chomp; print( $_ n ) if length == 8; } This version uses the diamond operator to take input when
        Message 3 of 10 , Apr 5, 2006
          On Wed, Apr 05, 2006 at 07:38:45PM -0000, a_z0_9_blah wrote:
          >
          > open(MYFILE, "example.txt") || die "Can't open myfile: $!";
          >
          > while (my $word = <MYFILE>) {
          > chomp $word; # remove the newline
          > print if length $word == 8;
          > }

          Fore!!!

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

          This version uses the diamond operator to take input when calling the
          script. Thus, if you save this file as len8, and you were operating on
          a file called words.txt, you would enter the following at the command
          line, assuming you made the file executable:

          $ len8 words.txt

          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.

          hope that helps

          --
          Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
          "A script is what you give the actors. A program
          is what you give the audience." - Larry Wall
        • Mr. Shawn H. Corey
          ... That s because some older operating systems do not read the shebang and the flag would be missed. Now if someone would come up with: use taint; -- __END__
          Message 4 of 10 , Apr 5, 2006
            On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
            > "use warnings;" seems to be the new Right Way To Do It, but I've yet to
            > see an explanation for why it's preferable to -w. What are the reasons
            > for preferring "use warnings;" over -w?
            >

            That's because some older operating systems do not read the shebang and
            the flag would be missed.

            Now if someone would come up with: use taint;


            --
            __END__

            Just my 0.00000002 million dollars worth,
            --- Shawn

            "For the things we have to learn before we can do them, we learn by doing them."
            Aristotle

            * Perl tutorials at http://perlmonks.org/?node=Tutorials
            * A searchable perldoc is at http://perldoc.perl.org/
          • Chad Perrin
            ... Ahh, that s a good point. I keep forgetting the limitations of OSes like Windows. -- Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ] Real
            Message 5 of 10 , Apr 5, 2006
              On Wed, Apr 05, 2006 at 06:10:11PM -0400, Mr. Shawn H. Corey wrote:
              > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
              > > "use warnings;" seems to be the new Right Way To Do It, but I've yet to
              > > see an explanation for why it's preferable to -w. What are the reasons
              > > for preferring "use warnings;" over -w?
              > >
              >
              > That's because some older operating systems do not read the shebang and
              > the flag would be missed.
              >
              > Now if someone would come up with: use taint;

              Ahh, that's a good point. I keep forgetting the limitations of OSes
              like Windows.

              --
              Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
              "Real ugliness is not harsh-looking syntax, but having to
              build programs out of the wrong concepts." - Paul Graham
            • a_z0_9_blah
              ... yet to ... reasons ... and ... see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are several issues involved here. I m not really clear on
              Message 6 of 10 , Apr 5, 2006
                --- In perl-beginner@yahoogroups.com, "Mr. Shawn H. Corey"
                <shawnhcorey@...> wrote:
                >
                > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
                > > "use warnings;" seems to be the new Right Way To Do It, but I've
                yet to
                > > see an explanation for why it's preferable to -w. What are the
                reasons
                > > for preferring "use warnings;" over -w?
                > >
                >
                > That's because some older operating systems do not read the shebang
                and
                > the flag would be missed.

                see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are
                several issues involved here. I'm not really clear on this, but it can
                have an undesired effect with use, do or require of other files.

                Chris
              • Chad Perrin
                ... Thanks. I ll read that. -- Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ] There comes a time in the history of any project when it becomes
                Message 7 of 10 , Apr 5, 2006
                  On Wed, Apr 05, 2006 at 11:59:31PM -0000, a_z0_9_blah wrote:
                  > --- In perl-beginner@yahoogroups.com, "Mr. Shawn H. Corey"
                  > <shawnhcorey@...> wrote:
                  > >
                  > > On Wed, 2006-05-04 at 14:12 -0600, Chad Perrin wrote:
                  > > > "use warnings;" seems to be the new Right Way To Do It, but I've
                  > yet to
                  > > > see an explanation for why it's preferable to -w. What are the
                  > reasons
                  > > > for preferring "use warnings;" over -w?
                  > > >
                  > >
                  > > That's because some older operating systems do not read the shebang
                  > and
                  > > the flag would be missed.
                  >
                  > see perllexwarn - Perl Lexical Warnings, in the Perl docs. There are
                  > several issues involved here. I'm not really clear on this, but it can
                  > have an undesired effect with use, do or require of other files.

                  Thanks. I'll read that.

                  --
                  Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
                  "There comes a time in the history of any project when it becomes necessary
                  to shoot the engineers and begin production." - MacUser, November 1990
                • 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 8 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
                    http://en.wikibooks.org/wiki/Programming:Perl_User_I/O

                    > 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.