读取文本文件并将其转换为哈希,以及访问值的 Perl 代码

huangapple go评论53阅读模式
英文:

Reading text file into hash and accessing values perl

问题

我尝试将文本文件内容读入散列中,但在读取和访问时遇到了一些问题。

resctrl_top
    /path/to/a/
vdm05top
    /path/to/b/
    /path/to/c/
    /path/to/d/
    /path/to/e/
    /path/to/f/

文件格式如上所示。我期望的输出是一个散列,其中非空行作为键,路径行作为值。我还想知道如何访问不同键的每个值。

resctrl_top => /path/to/a/
vdm05top => /path/to/b/,/path/to/c/,...

以下是我尝试过的努力:

use strict;
use warnings;
my %hash;
open FILE, "filename.txt" or die $!;
my $key;
while (my $line = <FILE>) {
     chomp($line);
     if ($line !~ /^\s/) {
        ($key) = $line =~ /^\S+/g;
        $hash{$key} = [];
     } else {
        $line =~ s/^\s+//;
        push @{ $hash{$key} }, $line;
     }
 }
 close FILE;

 foreach (keys %hash){
    print "$_ => " . join(',', @{$hash{$_}}) . "\n";
 }

希望这对你有所帮助。

英文:

I am trying to read text file content into hash but having some problem reading as well as accessing it.

resctrl_top
    /path/to/a/
vdm05top
    /path/to/b/
    /path/to/c/
    /path/to/d/
    /path/to/e/
    /path/to/f/

The file format will be as above. My desired output is a hash with the non spacing line as key, and the path lines as values. I would like to know also how to access each values for different keys.

resctrl_top =&gt; /path/to/a/
vdm05top =&gt; /path/to/b/,/path/to/c/,...

Below are the effort I tried:

use strict;
use warnings;
my %hash;
open FILE, &quot;filename.txt&quot; or die $!;
my $key;
while (my $line = &lt;FILE&gt;) {
     chomp($line);
     if ($line !~ /^\s/) {
        ($key) = $line =~ /^\S+/g;
        $hash{$key} = [];
     } else {
        $line =~ s/^\s+//;
        push @{ $hash{$key} }, $line;
     }
 }
 close FILE;

 foreach (keys %hash){
 	print &quot;$key =&gt; $hash{$key}\n&quot;;
 }

答案1

得分: 3

尝试这种方式:

use strict;
use warnings;
use Data::Dumper;
my %hash;
my $key;
while (my $line = <DATA>) {
     chomp($line);
     if ($line !~ /^\s/) {
        $key = $line;
     } else {
        $line =~ s/\s//g;
        push (@{$hash{$key}} , $line);
     }
}
my %final;
foreach my $k (keys %hash){
        my $val = join(",", @{$hash{$k}});
        $final{$k} = $val; #新散列将包含键和相应的值
}

print Dumper(\%final);

__DATA__
resctrl_top
    /path/to/a/
vdm05top
    /path/to/b/
    /path/to/c/
    /path/to/d/
    /path/to/e/
    /path/to/f/      

结果:

$VAR1 = {
          'vdm05top' => '/path/to/b/,/path/to/c/,/path/to/d/,/path/to/e/,/path/to/f/',
          'resctrl_top' => '/path/to/a/'
        };

希望这解决了你的问题。

英文:

Try this way:

use strict;
use warnings;
use Data::Dumper;
my %hash;
my $key;
while (my $line = &lt;DATA&gt;) {
     chomp($line);
     if ($line !~ /^\s/) {
        $key = $line;
     } else {
        $line =~ s/\s//g;
        push (@{$hash{$key}} , $line);
     }
}
my %final;
foreach my $k (keys %hash){
        my $val = join(&quot;,&quot;, @{$hash{$k}});
        $final{$k} = $val; #New hash will have key and respective values
}

print Dumper(\%final);

__DATA__
resctrl_top
    /path/to/a/
vdm05top
    /path/to/b/
    /path/to/c/
    /path/to/d/
    /path/to/e/
    /path/to/f/      

Result:

$VAR1 = {
          &#39;vdm05top&#39; =&gt; &#39;/path/to/b/,/path/to/c/,/path/to/d/,/path/to/e/,/path/to/f/&#39;,
          &#39;resctrl_top&#39; =&gt; &#39;/path/to/a/&#39;
        };

Hope this solves your problem.

答案2

得分: 0

这是一个相当简单的解决方案

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use feature 'say';
    
    use Data::Dumper; # 仅用于输出
    
    my ($key, %hash); # 声明全局变量
    
    while (<DATA>) {  # 快速处理 - 从 DATA 读取
      chomp;
    
      if (/^\s/) {    # 如果行以空格开头
        s/^\s+//;
        push @{$hash{$key}}, $_;
      } else {        # 该行是一个键
        $key = $_;
      }
    }
    
    say Dumper \%hash;
    
    __DATA__
    resctrl_top
        /path/to/a/
    vdm05top
        /path/to/b/
        /path/to/c/
        /path/to/d/
        /path/to/e/
        /path/to/f/
英文:

Here's a pretty simple solution.

#!/usr/bin/perl

use strict;
use warnings;
use feature &#39;say&#39;;

use Data::Dumper; # Just for output

my ($key, %hash); # Declare globals

while (&lt;DATA&gt;) {  # Quick hack - read from DATA
  chomp;

  if (/^\s/) {    # If the line starts with a space
    s/^\s+//;
    push @{$hash{$key}}, $_;
  } else {        # The line is a key
    $key = $_;
  }
}

say Dumper \%hash;

__DATA__
resctrl_top
    /path/to/a/
vdm05top
    /path/to/b/
    /path/to/c/
    /path/to/d/
    /path/to/e/
    /path/to/f/

huangapple
  • 本文由 发表于 2023年2月8日 14:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定