in reply to Scalar value @array better written as

Also, and possibly the cause of your second error,
my @subdirs = readdir($dh);
should be
my @subdirs = grep { /^\.\.?\z/ } readdir($dh);
to skip . and ...

Replies are listed 'Best First'.
Re^2: Scalar value @array better written as
by erhan (Novice) on Mar 09, 2015 at 19:25 UTC

    @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:  <%-(-(-(-<