distro has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm beginning in perl and I'm having some trouble with a program. I need this program to be able to find a file that a user puts in (STDIN, I know) and then find the amount of times a word that they also choose in the file they have chosen... This is what I have so far, sorry if it's completely wrong.

I know it's a bit scattered but I've just been fiddling with it for hours trying to get it to work.

#!/usr/bin/perl -w use strict; print "Please enter the name of the file to search:"; while (<>){ print; } print "Enter a word to search for:"; chomp( my $input = <STDIN>; my $ctr=0; foreach($filename){ if (/\b$input\b/){ $ctr++; } } print "Frequency: $ctr\n"; exit;
  • Comment on find the number of words that a user has chosen for an input file that a user has also chosen
  • Download Code

Replies are listed 'Best First'.
Re: find the number of words that a user has chosen for an input file that a user has also chosen
by tybalt89 (Monsignor) on Nov 16, 2016 at 22:27 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1176022 use strict; use warnings; chomp( my $filename = <STDIN> ); chomp( my $word = <STDIN> ); print "Frequency: " . grep $word eq $_, split /\W+/, do { local (@ARGV, $/) = $filename; <> };
Re: find the number of words that a user has chosen for an input file that a user has also chosen
by GrandFather (Saint) on Nov 16, 2016 at 22:52 UTC

    Before you present your code check that it at least runs, or ensure that you have copied and pasted (not retyped) the code that you want us to check. Your current code has obvious syntax errors that you would have been told about if you tried to run it such as the missing ')' in the chomp line and the fact that you use $filename before declaring it or giving it a value.

    Syntax errors aside, what do you expect the code to do and how does it achieve that? If you can't explain what you expect your code to do there's a pretty good chance the Perl interpreter is going to have a hard time of it too!

    Tell us what input is expected (give a little sample) and what output you expect (give a little sample) and show us what you actually get. If you can avoid needing to hand feed the script while it's running that helps too. See I know what I mean. Why don't you?.

    Premature optimization is the root of all job security

      I'm sorry, I am actually trying to learn though so I don't want to be hand fed. And I did forget to copy a portion, my apologies.

      EXAMPLE SCENARIO WITH MY CODE:

      User runs program, gets random intro message

      User is prompted for a file

      User types in 'usdeclaration.txt'

      User is prompted for a word

      User types in 'the'

      User sees message as 'Frequency: x'

      DIFFERENT SCENARIO: (includes more than I'm asking)

      User runs program, gets random intro message

      User is prompted for a file

      User types in 'gdfsagrwa.txt'

      (Let's say this file doesn't exist)

      Print error message saying file doesn't exist or whatever.

      I'm more concerned with the second scenario just because I have kind of figured out most of it since posting this.

        I didn't make my "hand fed" comment clear. I'm lazy. When testing your code I want to copy and paste your code into my IDE and run it without having to type any input - I don't want to "hand feed" your running code test input (I'll probably type it wrong cos I can't type either). So think about how you can test the code you are having trouble with without needing any extra input or files, then post that test code.

        Premature optimization is the root of all job security
Re: find the number of words that a user has chosen for an input file that a user has also chosen
by 1nickt (Canon) on Nov 16, 2016 at 22:29 UTC

    You don't need a script for that. You can get your count on the command line:

    $ cat 1176022.txt The foo bar all of the foo's men Not here Foo foo foo Notfoo What the foo Expecting five foos $
    $ perl -nE '$l++; $i += () = $_ =~ /\bfoo\b/g; say "After line $l: $i" +' 1176022.txt After line 1: 1 After line 2: 2 After line 3: 2 After line 4: 4 After line 5: 4 After line 6: 5 After line 7: 5 $

    See perlrun for Perl's command line switches.


    The way forward always starts with a minimal test.
      I know, but I want a program for easy repetition and access whenever.