recursivity

Discussion in 'Perl Misc' started by julia, Apr 4, 2005.

  1. julia

    julia Guest

    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"}



    }
    }
    julia, Apr 4, 2005
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Apr 4, 2005
    #2
    1. Advertising

  3. julia wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Apr 4, 2005
    #3
  4. julia <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 5, 2005
    #4
  5. John W. Krahn wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Apr 5, 2005
    #5
  6. julia

    John Bokma Guest

    John Bokma, Apr 5, 2005
    #6
  7. julia

    Guest

    John W. Krahn <> wrote:
    > 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
    , Apr 5, 2005
    #7
  8. julia

    Joe Smith Guest

    julia wrote:

    > 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
    Joe Smith, Apr 5, 2005
    #8
  9. julia

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > John W. Krahn <> wrote:
    > > 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.

    ^^^^
    $dir

    Anno
    Anno Siegel, Apr 5, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Orion93

    prob with Folders recursivity

    Orion93, Feb 23, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    70
    gnari
    Feb 23, 2004
Loading...

Share This Page