Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Open a folder

by Dr Manhattan (Beadle)
on Jan 08, 2013 at 14:35 UTC ( #1012240=note: print w/ replies, xml ) Need Help??


in reply to Re: Open a folder
in thread Open a folder

Hi, thank you for the help. I am not completely sure what your code is does, but the

next if $entry eq '.'; next if $entry eq '..';

did help. I have combined your code with something of another user to come up with this:

#!/usr/bin/perl -w use strict; use warnings; open (Output, ">Output.txt") or die "Can't open"; my $tmp_dir= 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $tmp_dir) or die $!; while (my $file_name = readdir(DIR)) { next if $file_name eq '.'; next if $file_name eq '..'; print "$file_name\n"; } close (Output) closedir(DIR);

All it does is print all the file names in my directory as output(so at least I am on the right path). However I want to iterate through each text file and do some basic calculations(all in 1 sub) within each file. Any ideas?


Comment on Re^2: Open a folder
Select or Download Code
Re^3: Open a folder
by blue_cowdawg (Prior) on Jan 08, 2013 at 14:51 UTC
        However I want to iterate through each text file and do some basic calculations(all in 1 sub) within each file.

    Add the sub to your code thusly:

    | in your while loop invoke: process_file($tmp_dir,$file); | and then after your loop sub process_file { my($path,$node)=@_; my $fname = $path . '\' . $node; open FIN,"<$fname" or die "$fname:$!"; while (my $line=<FIN>){ chomp $line; | do something here. } }


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

      Hi blue cowdawg

      I tried this:

      #!/usr/bin/perl -w use strict; use warnings; my $dir = 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $dir) or die $!; while (my $file = readdir(DIR)) { next if $file eq '.'; next if $file eq '..'; &countWords($dir, $file); #call sub } sub countWords { my $line = @_; #receive file as input my @array = split(/ /, $line); print "$#array\n"; } closedir(DIR);

      It is the same thing as earlier, I just added a small sub which is supposed to parse each file into an array, count the words and print the output(total words). Did I call the sub correctly? And does the sub receive the file correctly? When I run the script it gives the same answer(total words) for each text file, which is incorrect.

      Thank you

            And does the sub receive the file correctly? When I run the script it gives the same answer(total words) for each text file, which is incorrect.

        Professor Cowdawg just drew a red "X" across your assignment.

        sub countWords { 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; }
        You are not counting anything in your sub at all. You actually have to read the file in order to count the words in it.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re^3: Open a folder
by ww (Bishop) on Jan 08, 2013 at 14:53 UTC
    see perldoc -f push and Tutorials (below the text explaining how to/when to post a new tut) or Super Search re iterating through arrays.

    IOW, collect all the relevant $file_name entries into an array; at that point, iterating thru the individual files requires only that you attempt to inform yourself thru study of the references above.

    Update: Of course (/me says belatedly), you'll need to consider executing your script from an appropriate directory or troubling yourself to learn about perldoc -f chdir, perldoc cwd, etc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2014-07-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (165 votes), past polls