Well,
All of the criticism is well deserved. I must overcome my laziness and do as much as I can for myself. Thank you monks for the corrections. I truly wish I had a teacher to ask questions from, but I really am trying to learn Perl on my own. This is not a school assignment.
Thank you to L~R as your code sample really helped me to get started. However, I am struggling with find::file so I went back to “Learning Perl” and came up with this:
#!/usr/bin/perl -w
use strict;
#declare vars
my $dir_to_process; #sclar to hold directory location
my $file; #sclar to hold file names
my %stuff_to_store; #hash of directory locations and file names
$dir_to_process = "." ; #defining starting directory location
opendir DH,$dir_to_process or die "Cannot open $dir_to_process for pro
+cessing: $!"; #opening dir to process
foreach $file (readdir DH) { #cycling through the directory
if ($file =~ /\.txt$/) { #looking for file ext .txt
open(my $fh, $file) or die "Unable to open '$file' for reading
+: $!"; #reading .txt files
while (<$fh>) { #cycling through to find happy
if (/happy/) { #if its happy then store it in hash stuff_t
+o_store with file location
%stuff_to_store = ($dir_to_process, $file);
while (%stuff_to_store) { #see if it worked and print
+out each element of hash stuff_to_store
print; #its not working and I don't know why
}
}
}
close ($fh);
}
}
closedir DH;
I am trying my self imposed assignement in chunks. Can someone help me figure out why it is not working? Thanks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.