erhan has asked for the wisdom of the Perl Monks concerning the following question:

I have a few sub-folders in the main folder. There is a .txt file in each sub-folder. Firstly the code will create the "result" folder in the main folder. And it will search "atom" word in each .txt and print the first column including "atom" word. The code creates the "result" folder but it gives the following error. How can I fix it?

Error: Scalar value @fields1[1] better written as <c>$fields1[1] at ./ +analysis.pl line 32. Failed to open "./input.txt" for writing: No such file or directory at + ./analysis.pl line 27
#!/usr/bin/env perl use strict; use warnings; use File::Path qw/make_path/; use Cwd; my $dir = cwd(); opendir (my $dh, $dir) or die "Unable to open current directory! $!\n" +; my @subdirs = readdir $dh or die "Unable to read directory! $!\n"; closedir $dh; my $result_path = "$dir/results"; make_path("$result_path"); for my $subdir ( sort @subdirs ) { chdir($dir); next unless -d $subdir; make_path("$result_path/$subdir"); my $search_text1 = qr/atom/; my $infile1="$subdir/input.txt"; my $outfile1="$result_path/output"; open my $in_fh1, '<', $infile1 or die qq{Failed to open "$infile1 +" for writing: $!}; open my $out_fh1, '>', $outfile1 or die qq{Failed to open "$outfil +e1" for writing: $!}; while (<$in_fh1>) { next unless /$search_text1/; my @fields1 = split; print $out_fh1 join("\t", @fields1[1]), "\n"; } close($in_fh1); close($out_fh1); chdir(".."); }

Replies are listed 'Best First'.
Re: Scalar value @array better written as
by ikegami (Patriarch) on Mar 09, 2015 at 18:09 UTC

    For Scalar value @fields1[1] better written as $fields1[1], change @fields1[1] to $fields1[1].

    Failed to open "./input.txt" for writing: No such file or directory comes from your attempt to open a file that doesn't exist for reading. Maybe you want to skip that directory?

    for my $subdir ( sort @subdirs ) { chdir($dir); ... my $in_fh1; if (!open $in_fh1, '<', $infile1) { goto NEXT if $!{ENOENT}; die qq{Failed to open "$infile1" for reading: $!}; } ... NEXT: chdir('..'); }
Re: Scalar value @array better written as
by ikegami (Patriarch) on Mar 09, 2015 at 18:14 UTC
    Also, and possibly the cause of your second error,
    my @subdirs = readdir($dh);
    should be
    my @subdirs = grep { /^\.\.?\z/ } readdir($dh);
    to skip . and ...

      @ikegami thanks your reply. Now it gives an error:

      Scalar value @fields11 better written as $fields11 at ./analysis.pl line 40. And my input files are like this:
      Harmonic frequencies (cm**-1), IR intensities (KM/Mole), Raman scatte +ring activities (A**4/AMU), depolarization ratios for plane and unpolarize +d incident light, reduced masses (AMU), force constants (mDyne/A), and normal coordinates: 1 2 3 A A A atom -- 23.5214 40.9023 56.78 +56 Red. masses -- 6.7793 1.0546 5.56 +74 Frc consts -- 0.0022 0.0010 0.01 +06 IR Inten -- 2.4504 0.2236 0.61 +52

      New version:

      #!/usr/bin/env perl use strict; use warnings; use File::Path qw/make_path/; use Cwd; my $dir = cwd(); opendir (my $dh, $dir) or die "Unable to open current directory! $!\n" +; my @subdirs = grep { /^\.\.?\z/ } readdir($dh) or die "Unable to read +directory! $!\n"; closedir $dh; my $result_path = "$dir/results"; make_path("$result_path"); for my $subdir ( sort @subdirs ) { chdir($dir); next unless -d $subdir; make_path("$result_path/$subdir"); my $search_text1 = qr/atom/; my $infile1="$subdir/input.txt"; my $in_fh1; if (!open $in_fh1, '<', $infile1) { goto NEXT if $!{ENOENT}; die qq{Failed to open "$infile1" for reading: $!}; } my $outfile1="$result_path/output"; open $in_fh1, '<', $infile1 or die qq{Failed to open "$infile1" f +or writing: $!}; open my $out_fh1, '>', $outfile1 or die qq{Failed to open "$outfil +e1" for writing: $!}; while (<$in_fh1>) { next unless /$search_text1/; my @fields1 = split; print $out_fh1 join("\t", @fields1[1]), "\n"; } close($in_fh1); close($out_fh1); NEXT: chdir(".."); }
        Now it gives an error...

        It still gives this "error" (not, in fact, an error but a warning) because the scalar expression  @fields1[1] (an array slice; see Slices in perldata) is still being used and is still better written as  $fields1[1] (please see above, first sentence).


        Give a man a fish:  <%-(-(-(-<

Re: Scalar value @array better written as
by ikegami (Patriarch) on Mar 09, 2015 at 18:11 UTC
    dup