recursivity

J

julia

Hello,

I am trying to write a script that read files in a directory that I
can supply at the command line. (split.pl in). The script aborted...
Please help me find how to fix the problem.
Thanks


use strict;
my $input_dir = $ARGV[0] || '.' ;

MAIN:

opendir DIR, $dir || die "Couldnt open $dir - $!\n";
my @entries = readdir(DIR);
closedir (DIR);

foreach my $file (@entries) {
my $filename = "$dir/$file";
if ( -f $filename ){
open(F, "<$filename");
}
}

while (<F>) {
my @words = split(/\W*\s+\W*/, $_); # split
foreach my $num ( 0 .. $#words) {
open OUT, ">out_$filename.txt" and select OUT if 1..1;
{print $num+1, "\t$words[$num] -- file $filename\n"}



}
}
 
J

John W. Krahn

julia said:
I am trying to write a script that read files in a directory that I
can supply at the command line. (split.pl in). The script aborted...
Please help me find how to fix the problem.
Thanks


use strict;
my $input_dir = $ARGV[0] || '.' ;

MAIN:

opendir DIR, $dir || die "Couldnt open $dir - $!\n";

Because of precedence that will never die. You need to either add parentheses
or use the lower precedence 'or' operator.

my @entries = readdir(DIR);
closedir (DIR);

foreach my $file (@entries) {
my $filename = "$dir/$file";
if ( -f $filename ){
open(F, "<$filename");
}
}

while (<F>) {
my @words = split(/\W*\s+\W*/, $_); # split
foreach my $num ( 0 .. $#words) {
open OUT, ">out_$filename.txt" and select OUT if 1..1;
{print $num+1, "\t$words[$num] -- file $filename\n"}

}
}


This may work better (untested):

use warnings;
use strict;
my $input_dir = $ARGV[0] || '.';

opendir DIR, $dir or die "Couldnt open $dir - $!\n";

{ local @ARGV = grep -f, map "$dir/$_", readdir DIR;

while ( <> ) {
if ( $. == 1 ) {
open OUT, '>', "out_$ARGV.txt" or die "Cannot open out_$ARGV.txt:
$!";
select OUT;
}
my @words = split /\W*\s+\W*/;
for my $num ( 1 .. @words ) {
print "$num\t$words[$num-1] -- file $ARGV\n"
}
}
}

__END__



John
 
T

Tad McClellan

julia said:
Subject: recursivity


There is no recursion anywhere in your code.

Did you mean to ask something about "recursivity", whatever
the heck that might mean?

The script aborted...


I doubt that it got even that far, as it won't even compile.

Please help me find how to fix the problem.


Declare and initialize the $dir variable.

my @words = split(/\W*\s+\W*/, $_); # split


Useless comments should be avoided.
 
J

John W. Krahn

John said:
This may work better (untested):

use warnings;
use strict;
my $input_dir = $ARGV[0] || '.';

opendir DIR, $dir or die "Couldnt open $dir - $!\n";

{ local @ARGV = grep -f, map "$dir/$_", readdir DIR;

while ( <> ) {
if ( $. == 1 ) {
open OUT, '>', "out_$ARGV.txt" or die "Cannot open
out_$ARGV.txt: $!";
select OUT;
}
my @words = split /\W*\s+\W*/;
for my $num ( 1 .. @words ) {
print "$num\t$words[$num-1] -- file $ARGV\n"
}

Oops, I forgot to reset $.

close ARGV if eof;
}
}

__END__




John
 
J

John Bokma

John said:
my $input_dir = $ARGV[0] || '.';

Now try 0 for a dir name...

Only use the || thing if you are 100% sure it can't cause weird accidents.

Imagine you have actually a dir called 0, and your script recursively
deletes all files...
 
A

axel

John W. Krahn said:
This may work better (untested):

The lack of testing shows.
opendir DIR, $dir or die "Couldnt open $dir - $!\n";

The variable $var is never declared.
opendir DIR, $dir or die "Couldnt open $dir - $!\n";

{ local @ARGV = grep -f, map "$dir/$_", readdir DIR;

while ( <> ) {
if ( $. == 1 ) {
open OUT, '>', "out_$ARGV.txt" or die "Cannot open out_$ARGV.txt:

You have mapped the filenames to include the directory, so e.g. if $ARGV
here is 'foo/bar' you are trying to write to the file 'bar' in the
directory 'out_foo'.

Axel
 
J

Joe Smith

julia said:
opendir DIR, $dir || die "Couldnt open $dir - $!\n";
my @entries = readdir(DIR);
closedir (DIR);

foreach my $file (@entries) {

use File::Find;
find(\&wanted,@ARGV);
exit;

sub wanted {
print "Processing file '$_' in directory $File::Find::dir\n";
# Use $_ or $File::Find::name when opening the input file.
}

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Very Sluggish Code 4
Help with Hash 2
Merge files 1
prob with Folders recursivity 1
Efficiently searching multiple files 10
Sorting 3
Withopen module and mainsheet cycles 11
issue with closedir 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top