Regex 在perl中,如何读取文件中的数据,该文件包含在文件中?

Regex 在perl中,如何读取文件中的数据,该文件包含在文件中?,regex,perl,Regex,Perl,我有list2.txt文件,在该文件中有几个文件,如02x5.txt或0.3x5.txt等。那么如何一次读取02x5.txt文件中的数据?在02x5.txt内部,有高度:20,长度:5,颜色:蓝色等 inside list2.txt: 02x5.txt 03x5.txt inside 02x5.txt: height:20, length:5, colour:blue inside 03x5.txt: height:25, length:10, colour:green #/us

我有list2.txt文件,在该文件中有几个文件,如
02x5.txt
0.3x5.txt
等。那么如何一次读取
02x5.txt
文件中的数据?在
02x5.txt
内部,有
高度:20,长度:5,颜色:蓝色

inside list2.txt:
02x5.txt
03x5.txt


inside 02x5.txt:
height:20, 
length:5, 
colour:blue

inside 03x5.txt:
height:25, 
length:10, 
colour:green
#/usr/bin/perl
严格使用;
使用警告;
#从文件(或者更确切地说,从文件句柄)读取一行
我的$filename=“list2.txt”;

如果(打开我的$data,“请参阅下面执行您描述的任务的代码段,读取存储在哈希
%data
中的数据,您可以按照自己的意愿使用这些数据

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my $debug = 1;

my $filename = 'list2.txt';

my %data;

open my $fh, '<', $filename
    or die "Couldn't open $filename";

my @filenames = <$fh>;

close $fh;

chomp @filenames;

foreach $filename(@filenames) {
    open $fh, '<', $filename
        or die "Couldn't open $filename";

    while( <$fh> ) {
        chomp;
        my($k,$v) = split ':';
        $data{$filename}{$k} = $v;
    }

    close $fh;
}

say Dumper(\%data);

答案总是把问题分解成小块

#!/usr/bin/perl

use strict;
use warnings;

my @files = get_list_of_files();

my %data;

foreach my $file (@files) {
  my $file_data = get_data_from_file($file);

  $data{$file} = $file_data;
}

# You now have your data in a hash called %data.
# Do whatever you want with it.

sub get_list_of_files {
  open my $list_fh, '<', 'list2.txt'
    or die $!;

  my @files = <$list_fh>;
  chomp(@files);

  return @files;
}

sub get_data_from_file {
  my ($filename) = @_;

  my $record;

  open my $fh, '<', $filename or die $!;

  while (<$fh>) {
    chomp;
    # Remove trailing comma
    s/,$//;
    my ($key, $value) = split /:/;
    $record->{$key} = $value;
  }

  return $record;
}
!/usr/bin/perl
严格使用;
使用警告;
my@files=获取_文件的_列表();
我的%数据;
foreach my$文件(@files){
my$file\u data=从文件($file)获取数据;
$data{$file}=$file\u数据;
}
#现在,您的数据位于名为%data的散列中。
#你想用它做什么就做什么。
子获取\u文件的\u列表\u{

打开我的$list\u fh,'list2.txt真的是一个文本文件吗?是的,list2.txt中有一堆txt文件。或者,list2.txt是否包含文本文件的名称,而不是文件本身?为什么要选择并打印一个额外的换行符?只需将换行符保留在
$row
中。这不会影响匹配。一旦您在
list2.txt
然后您还需要打开该文件(使用其自己的文件句柄),以便从中读取
$VAR1 = {
          '02x5.txt' => {
                          'colour' => 'blue',
                          'height' => '20, ',
                          'length' => '5, '
                        },
          '03x5.txt' => {
                          'length' => '10, ',
                          'colour' => 'green',
                          'height' => '25, '
                        }
        };
#!/usr/bin/perl

use strict;
use warnings;

my @files = get_list_of_files();

my %data;

foreach my $file (@files) {
  my $file_data = get_data_from_file($file);

  $data{$file} = $file_data;
}

# You now have your data in a hash called %data.
# Do whatever you want with it.

sub get_list_of_files {
  open my $list_fh, '<', 'list2.txt'
    or die $!;

  my @files = <$list_fh>;
  chomp(@files);

  return @files;
}

sub get_data_from_file {
  my ($filename) = @_;

  my $record;

  open my $fh, '<', $filename or die $!;

  while (<$fh>) {
    chomp;
    # Remove trailing comma
    s/,$//;
    my ($key, $value) = split /:/;
    $record->{$key} = $value;
  }

  return $record;
}