文件目录不断变化的Perl
File directory constantly changing Perl
我想从这个目录中的 data.list
中检索行数:Project/chris/company/Delta/force/run_20210619_df/total/data.list
但问题是 run_20210619_df
更改并在每次将新数据更新到 data.list.
假设原始目录是:Project/chris/company/Delta/force/run_20210619_df/total/data.list
但是今天data.list
正在更新,所以形成了一个新目录:Project/chris/company/Delta/force/run_20210624_df/total/data.list
并且两个目录都包含 data.list
我可以制作一个始终可以读取最新版本的脚本吗data.list
?
Delta and charlie
是检索 data.list 的不同目录,因为有许多 datal.list 可从
检索
根据某人的回答我得到了这个:
my $filepathABC = Project/chris/company/Delta/force/*/total/data.list
my $filepathBCD = Project/chris/company/Charlie/force/*/total/data.list
my $hashref;
my $path;
print "Enter path : ";
$path = <STDIN>;
chomp $path;
if ( $path eq "ABC" ) {
for my $file ( glob($filepathABC ) ) {
$hashref->{$file} = count_lines($file);
$curl and upload data here........
}
else ($path eq "BCD") {
for my $file ( glob($filepathBCD ) ) {
$hashref->{$file} = count_lines($file);
$curl and upload data here........
}
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count = grep { not ?^$|^\s*#/ }<$fh>;
close $fh;
printf "$count\n";
return $count;
}
不幸的是,当我 运行 脚本时,它无法检测到要从哪个 data.list 中提取信息。
OP 的问题不清楚,如果目录 run_20210619_df
在每次更新 data.list
文件时替换为新目录,或者在这个文件系统级别会有多个目录。
请调查以下演示代码片段,该代码片段在文件结构中查找 data.list
,其中 *
作为通配符代替 run_20210619_df
目录。
实现了一个简单的子例程 count_lines($fname)
来计算文件中的行数。
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $files = 'Project/chris/company/Delta/force/*/total/data.list';
my $hashref;
for my $file ( glob($files) ) {
$hashref->{$file} = count_lines($file);
}
say Dumper($hashref);
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count++ while <$fh>;
close $fh;
return $count;
}
根据 OP 的要求:更改代码以仅打印行数
for my $file ( glob($files) ) {
say count_lines($file);
}
更新版本:
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $files = './Project/chris/company/*/force/*/total/data.list';
my $hashref;
for my $file ( glob($files) ) {
my($id,$date) = (split('/',$_))[4,6];
if( $hashref->{$id) and $date gt $hashref->{$id}{date} ) {
$hashref->{$id}{file} = $file;
$hashref->{$id}{date} = $date;
$hashref->{$id}{count} = count_lines($file);
} else {
$hashref->{$id}{file} = $file;
$hashref->{$id}{date} = $date;
$hashref->{$id}{count} = count_lines($file);
}
}
say Dumper($hashref);
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count++ while <$fh>;
close $fh;
return $count;
}
描述:
更新后的代码依赖于提取 Delta
、Charlie
等路径的组件作为 $id
和 date_df
作为参考日期字符串 $date
.
然后我们构建哈希的哈希来分离每个 $id
的信息,这些信息将包含文件名、date_df 字符串和文件行数。
提取的 $date
与存储的 $hashref->{$id}{date}
的比较是通过字符串比较运算符 gt
实现的,如果重新发送新日期,则会更新哈希信息。
处理完所有文件后,存储在 $hashref
引用的散列中的最终结果可用于上传感兴趣的信息。
测试脚本以查看是否找到任何文件(如果 $files
指向正确的位置)。
use strict;
use warnings;
use feature 'say';
my $files = './Project/chris/company/*/force/*/total/data.list';
for my $file ( glob($files) ) {
say $file;
}
测试脚本
use strict;
use warnings;
use feature 'say';
my $files = './*/*/*.pl';
while( glob($files) ) {
say;
}
输出
./kodi/bin/category.pl
./perl/capacitor/capacitor.pl
./perl/db/test.pl
./perl/db/x_test.pl
./perl/examples/abc.pl
./perl/examples/abcd.pl
./perl/examples/ampl.pl
./perl/examples/arr_cc.pl
./perl/examples/arr_dup.pl
......
./perl/YouTube/test_channel.pl
./perl/YouTube/test_playlist.pl
./perl/YouTube/test_playlists.pl
./perl/YouTube/test_search.pl
./perl/YouTube/test_videos.pl
./php/x/map_file.pl
参考:
glob
我想从这个目录中的 data.list
中检索行数:Project/chris/company/Delta/force/run_20210619_df/total/data.list
但问题是 run_20210619_df
更改并在每次将新数据更新到 data.list.
假设原始目录是:Project/chris/company/Delta/force/run_20210619_df/total/data.list
但是今天data.list
正在更新,所以形成了一个新目录:Project/chris/company/Delta/force/run_20210624_df/total/data.list
并且两个目录都包含 data.list
我可以制作一个始终可以读取最新版本的脚本吗data.list
?
Delta and charlie
是检索 data.list 的不同目录,因为有许多 datal.list 可从
根据某人的回答我得到了这个:
my $filepathABC = Project/chris/company/Delta/force/*/total/data.list
my $filepathBCD = Project/chris/company/Charlie/force/*/total/data.list
my $hashref;
my $path;
print "Enter path : ";
$path = <STDIN>;
chomp $path;
if ( $path eq "ABC" ) {
for my $file ( glob($filepathABC ) ) {
$hashref->{$file} = count_lines($file);
$curl and upload data here........
}
else ($path eq "BCD") {
for my $file ( glob($filepathBCD ) ) {
$hashref->{$file} = count_lines($file);
$curl and upload data here........
}
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count = grep { not ?^$|^\s*#/ }<$fh>;
close $fh;
printf "$count\n";
return $count;
}
不幸的是,当我 运行 脚本时,它无法检测到要从哪个 data.list 中提取信息。
OP 的问题不清楚,如果目录 run_20210619_df
在每次更新 data.list
文件时替换为新目录,或者在这个文件系统级别会有多个目录。
请调查以下演示代码片段,该代码片段在文件结构中查找 data.list
,其中 *
作为通配符代替 run_20210619_df
目录。
实现了一个简单的子例程 count_lines($fname)
来计算文件中的行数。
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $files = 'Project/chris/company/Delta/force/*/total/data.list';
my $hashref;
for my $file ( glob($files) ) {
$hashref->{$file} = count_lines($file);
}
say Dumper($hashref);
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count++ while <$fh>;
close $fh;
return $count;
}
根据 OP 的要求:更改代码以仅打印行数
for my $file ( glob($files) ) {
say count_lines($file);
}
更新版本:
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $files = './Project/chris/company/*/force/*/total/data.list';
my $hashref;
for my $file ( glob($files) ) {
my($id,$date) = (split('/',$_))[4,6];
if( $hashref->{$id) and $date gt $hashref->{$id}{date} ) {
$hashref->{$id}{file} = $file;
$hashref->{$id}{date} = $date;
$hashref->{$id}{count} = count_lines($file);
} else {
$hashref->{$id}{file} = $file;
$hashref->{$id}{date} = $date;
$hashref->{$id}{count} = count_lines($file);
}
}
say Dumper($hashref);
sub count_lines {
my $fname = shift;
my $count;
open my $fh, '<', $fname or die $!;
$count++ while <$fh>;
close $fh;
return $count;
}
描述:
更新后的代码依赖于提取 Delta
、Charlie
等路径的组件作为 $id
和 date_df
作为参考日期字符串 $date
.
然后我们构建哈希的哈希来分离每个 $id
的信息,这些信息将包含文件名、date_df 字符串和文件行数。
提取的 $date
与存储的 $hashref->{$id}{date}
的比较是通过字符串比较运算符 gt
实现的,如果重新发送新日期,则会更新哈希信息。
处理完所有文件后,存储在 $hashref
引用的散列中的最终结果可用于上传感兴趣的信息。
测试脚本以查看是否找到任何文件(如果 $files
指向正确的位置)。
use strict;
use warnings;
use feature 'say';
my $files = './Project/chris/company/*/force/*/total/data.list';
for my $file ( glob($files) ) {
say $file;
}
测试脚本
use strict;
use warnings;
use feature 'say';
my $files = './*/*/*.pl';
while( glob($files) ) {
say;
}
输出
./kodi/bin/category.pl
./perl/capacitor/capacitor.pl
./perl/db/test.pl
./perl/db/x_test.pl
./perl/examples/abc.pl
./perl/examples/abcd.pl
./perl/examples/ampl.pl
./perl/examples/arr_cc.pl
./perl/examples/arr_dup.pl
......
./perl/YouTube/test_channel.pl
./perl/YouTube/test_playlist.pl
./perl/YouTube/test_playlists.pl
./perl/YouTube/test_search.pl
./perl/YouTube/test_videos.pl
./php/x/map_file.pl
参考: glob