Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Open a directory and recursively process files

by Dr Manhattan (Beadle)
on Jan 09, 2013 at 13:33 UTC ( #1012485=perlquestion: print w/ replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am pretty new to Perl. I have a script which is supposed to open a directory, and then open each file within the directory and parse it into an array using a sub.

#!/usr/bin/perl -w use strict; my $dir = 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { &process_file ($file); } sub process_file { my $line = @_; my @array = split(/ /, $line); print "$#array\n"; } closedir (DIR);

However I am not sure that my sub is receiving the text files correctly. I think it bombs out at the

my $line = @_;

line. Can anyone help me with receiving the text file in the sub please?

Comment on Open a directory and recursively process files
Select or Download Code
Re: Open a directory and recursively process files
by Corion (Pope) on Jan 09, 2013 at 13:35 UTC
Re: Open a directory and recursively process files
by blue_cowdawg (Prior) on Jan 09, 2013 at 14:06 UTC

    Dear Monk,
    Did I or did I not just answer this question here? Why are you reposting this with a different subject?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Hi blue cowdawg

      I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is    open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

      #!/usr/bin/perl -w use strict; my $dir = 'C:\Users\Zandre Botha\Desktop\Text Files'; opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { &process_file ($file); } sub process_file { my $fname=join("/",@_); open FIN,"< $fname" or die "$fname:$!"; my @lines=<FIN>; chomp @lines; close FIN; my $count=0; foreach my $line(@lines) { map { $count++ } split (/[\s\t\n]+/,$line); } printf "There are %d words in %s\n",$count,$fname; } closedir (DIR);

        Any idea why this happens?

        Because you don't have permission -- ask the computer why

        stat, file, ls

        or die Fudge( "q{$file}:" ); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }
            I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

        If you read the diagnostic carefully you'll notice it complains about the filename "." which is actually your current working directory. You forgot the line

        next if ($file eq '.') || ( $file eq '..');
        just prior to the call to the sub. I think I mentioned that a few answers ago...


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        You failed to include a couple of lines of code you were given the last time you asked this question. Guess which ones:Re^2: Open a folder
Re: Open a directory and recursively process files
by ww (Bishop) on Jan 09, 2013 at 14:36 UTC

    As Ln 14 is written, $line is a count of the elements in @line Processing that -- which is always a "1" as written is not quite the same as "bombs out." Precise description of the failures your code produces helps us help you.

    Writing Ln 14 in any of the following fashions does PART of what you appear to intend (with respect to the FILENAME, ONLY*):

    my $line = $_[0]; my $line = shift; (my $line) = @_;

    The first specifies that $line should be assigned the value (content) of element zero of @_
    The second does the same (cf perldoc -f shift)
    The third makes the assignment in the appropriate context (qv in "Learning Perl," http://perldoc.perl.org/perldata.html#Context, Context tutorial, or any number of other places).

    *However, since you seem to want to split the contents of each text file into an array of words, you'll have to do a good bit more in your sub -- namely, read the file whose name you've passed (slurping if the file is not overly large) and do the appropriate processing on that file --- all within the existing sub.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012485]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-07-28 23:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls