heh well the name sums it up, I originally made it to search through some chat logs to see how sentances I uses "heh" in a chat log I had... heh.
#!/usr/bin/perl -w #This Program will check to see on how many lines a word #or word fragment occurs print "Welcome to Uselessmaster 4000\n"; $op="S"; #Sets a Value to op so that there is no message about +it being uninitialized until ($op =~/Q/i) { print "Would you like to (S)earch a file or (Q)uit?\n"; $op=<STDIN>; if ($op =~/Q/i) { print "Just hit enter to shuffle though all prompts\n"; } print "enter the word you would like to check for\n"; chomp ($word=<STDIN>); print "enter the full pathname of the file you would like to search\n" +; $file=<STDIN>; open FILE, "<$file" or die "Sorry no luck opening file"; $totline=0; $occ=0; for $line (<FILE>) { $totline=$totline+1; if ($line =~/$word/i) {$occ=$occ+1;}; }; print "Of ",$totline," lines the word ",$word," occured on ",$occ," li +nes\n"; };
Ok after reading some of the suggestions here I decided to update the uselessmaster I would have prefered to post this in Seekers of Perl Wisdom but I decided instead that it would problably be a better idea just to edit my original post to include the second draft rather then clutter things up with a additional post, so heres the second version of the useless master.
#!/usr/bin/perl -w use strict; #This Program will check to see on how many lines a word or word fragm +ent occurs print "Welcome to Uselessmaster 4000\n"; my($totline,$op,$word,$file,$occ,$line); $op="S"; #Sets a Value to op so that there is no message about +it being uninitialized until ($op =~/Q/i) { print "enter the word you would like to check for\n"; chomp ($word=<STDIN>); print "enter the full pathname of the file you would like to search\n" +; $file=<STDIN>; open FILE, "<$file" or die "Sorry no luck opening file $!"; $totline=0; $occ=0; for $line (<FILE>) { $totline++; if ($line =~/$word/i) {$occ++;}; }; print "Of ",$totline," lines the word ",$word," occured on ",$occ," li +nes\n"; print "which is is ",$occ/$totline*100,"% of the lines\n\n"; print "Would you like to (S)earch another file or (Q)uit?\n"; $op=<STDIN>; };
Thank you all for the suggestions and I hope that the second version is a improvement over the first.

Replies are listed 'Best First'.
Re: The Useless Master 4000
by John M. Dlugosz (Monsignor) on Jul 04, 2001 at 01:41 UTC
    I think if you tested at the end of the loop, not the beginning, you would not have the problem with $op being undefined the first time.
    my $op; do { ... } until ($op =~/Q/i);
    because you really do want it to go through once.

    Also, instead of

    if ($op =~/Q/i) { print "Just hit enter to shuffle though all prompts\n"; }
    which annoys the user with a last useless pass, just say
    last if $op =~ /Q/i;
    And then you don't even need the test at the end (or beginning) at all.

    Also, are you aware of the ++ operator?

    Basically, this is a specialized grep. That's a good use for Perl, in general.

    —John

Re: The Useless Master 4000
by BrentDax (Hermit) on Jul 06, 2001 at 06:53 UTC
    I guess you're a newbie huh? That's okay. I'll just help you make this code a little more Perlish...
    #!/usr/bin/perl -w #This Program will check to see on how many lines a word #or word fragment occurs use strict; #well, you're already using -w print "Welcome to Uselessmaestro 4000\n"; # :^) my($totline, $occ, $op, $file, $word); while (1) { print "Would you like to (S)earch a file or (Q)uit?\n"; $op=<STDIN>; last if ($op =~/Q/i); print "Word: "; chomp($word=<STDIN>); print "Full path to file: "; chomp($file=<STDIN>); open FILE, "<$file" or die "Unable to open $file: $!"; undef($totline, $occ); while(<FILE>) { #a little more efficient--doesn't slurp in the wh +ole file at once, plus puts it in $_ so it's easy to search $totline++; $occ++ if (/$word/i); } print "Of $totline lines the word '$word' occured on $occ of them. +\n"; }
    Note that some of my changes were to fit my own style--if you don't like something, you can probably change it back. Feel free to ask if I changed something so you don't understand it--that's what I'm here for, after all. :^)

    =cut
    --Brent Dax

    @HPAJ=split("", "rekcaH lreP rentonA tsuJ"); print reverse @HPAJ; #sucky but who cares?