in reply to Re: Search and store solution?
in thread Search and store solution?

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.

Replies are listed 'Best First'.
Re^3: Search and store solution?
by Limbic~Region (Chancellor) on Jul 12, 2008 at 20:31 UTC
    nukeboy,
    However, I am struggling with find::file so I went back to “Learning Perl” and came up with this:

    Note that my code uses File::Find::Rule which has a very user friendly interface (as opposed to File::Find). I am not sure what was difficult to understand from my example but it does exactly what you asked for.

    I will comment on a few snippets of your code.

    opendir DH, $dir_to_process # Use a lexical ($dir) instead of a bareword global (DH) if possible
    if ($file =~ /\.txt$/) # This would break for a directory called dir.txt - see perldoc -f -X
    %stuff_to_store = ($dir_to_process, $file); # This overwrites the hash entirely every time
    while (%stuff_to_store) { # this will be an infinite loop if the hash contains any items

    Please note: Your code will only search the current directory - it will not descend into sub directories. Out of curiosity, how much time did you spend with the code I wrote before you gave up on it? It really does 97% of what you asked.

    Cheers - L~R

Re^3: Search and store solution?
by YYCseismic (Beadle) on Jul 14, 2008 at 17:40 UTC
    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.

    I myself am just learning Perl, and have just recently finished (my first reading of) the Llama. I've started now on the Alpaca (Intermediate Perl) and find the material interesting.

    My best suggestion for you if you are having difficulty learning Perl is to work through the exercises at the end of each chapter in the Llama. The project that prompted me to learn Perl covers some more advanced ideas, so I had to go out and look at complex data structures while still reading the Llama. But the further I got through the book, the more I found that would help with my project, and that I would never have thought of. This site can be a boon to the Perl initiate, but you have to be willing to do the work.

    So yes, you must overcome your laziness. Programmers are a lazy bunch, but (usually) only in the sense that they don't feel the need to re-invent the wheel over and over. Code reuse is one result of this laziness.

    Those are just some of my thoughts.

Re^3: Search and store solution?
by Anonymous Monk on Jul 11, 2008 at 16:15 UTC
    %stuff_to_store= (...,...) isn't the way to add something to your hash, it just initializes a hash. Your code will make sure there is always only one hash element in the hash as previous elements are overwritten. Use $stuff_to_store{$dir_to_process}= $file instead.

    Note that you are only storing one file per $dir_to_process here. If you want to have a list of files for every directory, you have to upgrade your code to use a hash of arrays for example

    while (%stuff_to_store) won't loop through your hash, it will loop as long as anything is stored in the hash and since you don't change the hash in the loop that means forever. Try out print scalar %stuff_to_store; to see what I mean. Use

    foreach (keys %stuff_to_store) { print $_,' ',$stuff_to_store{$_},"/n"; }
    to loop through and print out the hash.

    "its not working" is the worst problem description you can make. Try to be more specific by telling what your script prints out and what it should print out.