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(".."); }
In reply to Scalar value @array better written as by erhan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |