in reply to selcting a specific file

my @nonempty_files = grep { ! -z $_ } @files;

Replies are listed 'Best First'.
Re^2: selcting a specific file
by mwah (Hermit) on Oct 22, 2007 at 21:25 UTC

    this:

    my @nonempty_files = grep { ! -z $_ } @files;

    looks not very trustworthy ;-). This might be wrong. Compare some test schenarios with this approach:

    ... my @nonempty_files = grep -e && !-z, @files; ...

    (especially for non-accessible and non-existent files.)

    Regards

    mwa

Re^2: selcting a specific file
by Anonymous Monk on Oct 22, 2007 at 21:22 UTC
    this is the original code
    @files = grep { /\.log$/ } readdir ($DIR); foreach my $x (@files){ open READFILE, "......"; open WRITEFILE, ">......"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE;

      Your code won't compile. That's not very dire. Just stay comitted to it ;-)

      use strict; use warnings; my $path = '/tmp/allfiles'; my $outpath = '/tmp/nonemptyfiles'; opendir my $dh, $path or die $!; my @files = grep /\.log$/ && -f "$path/$_" && !-z "$path/$_", readdir +$dh; closedir $dh; for my $fn (@files){ open my $fr, '<', "$path/$fn" or die "IN: $fn $!"; open my $fw, '>', "$outpath/$fn" or die "OUT: $fn $!";; while( <$fr> ) { printf $fw "$1 $2\n" if /(\S+)\s+(\S+)/ } }

      Try to study the example and bear some idomatic expressions in mind like indirect file handles and checks for success ...

      Regards

      mwa

        #!/usr/bin/perl $dest="/home/perl/sra/"; print "the source direcotry name:\n"; chomp(my $source=<STDIN>); opendir($DIR, $source) or die "cannot open the directory '$source': $!"; @files =@files = grep { /\.log$/ } readdir ($DIR); foreach my $x (my @files){ open READFILE, "$source/$x"; open WRITEFILE, ">$dest$x"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE; } closedir($DIR)
        this code works out but the options given by you for selecting and copying only non empty files doesnt work out!! please help me out!!
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: selcting a specific file
by Anonymous Monk on Oct 22, 2007 at 21:26 UTC
    @files = grep { /\.c$/ } readdir ($DIR);
    opening a dir a selcting .c files !! how to use this !-z!!!