【问题标题】:Perl keys and multiple values queryPerl 键和多值查询
【发布时间】:2014-09-25 21:46:33
【问题描述】:

能否请您帮助我如何在 perl 中获取具有不同值的唯一键?这些日志文件经常生成并且值不断变化。

"cloning": true
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14421
"frontend_ppid": 1
"git_dir": "/repositories/xorg/myrepo.git"
"hostname": "github.com"
"pgroup": "20603"
"pid": 20603
"ppid": 20600
"program": "upload-pack"

"cloning": false
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14422
"frontend_ppid": 2
"git_dir": "/repositories/yorg/myrepo2.git"
"hostname": "github.com"
"pgroup": "20604"
"pid": 20604
"ppid": 20500
"program": "upload-pack"

提前致谢

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试,另请参阅how to ask a good question on Stack Overflow
  • 这不是 Perl 哈希。它看起来更像一个 JSON 对象,但缺少逗号。还是这部分是日志文件?
  • 嗨,这是 github 审核日志的一部分,我正在尝试获取具有多个值的唯一键。我无法在这里复制我的代码,到目前为止我已经完成了,我会再试一次,谢谢大家的帮助

标签: perl


【解决方案1】:
use Data::Dump;

my %h;
while (my $line = <DATA>) {
  next if $line !~ /\S/;

  my @r = split /:/, $line;
  s/^["\s]+ | ["\s]+//xg for @r;

  push @{ $h{$r[0]} }, $r[1];
}

dd \%h;

__DATA__
"cloning": true
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14421
"frontend_ppid": 1
"git_dir": "/repositories/xorg/myrepo.git"
"hostname": "github.com"
"pgroup": "20603"
"pid": 20603
"ppid": 20600
"program": "upload-pack"

"cloning": false
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14422
"frontend_ppid": 2
"git_dir": "/repositories/yorg/myrepo2.git"
"hostname": "github.com"
"pgroup": "20604"
"pid": 20604
"ppid": 20500
"program": "upload-pack"

输出

{
  cloning       => ["true", "false"],
  cmdline       => ["git-upload-pack", "git-upload-pack"],
  features      => ["", ""],
  frontend      => ["github.com", "github.com"],
  frontend_pid  => [14421, 14422],
  frontend_ppid => [1, 2],
  git_dir       => [
                     "/repositories/xorg/myrepo.git",
                     "/repositories/yorg/myrepo2.git",
                   ],
  hostname      => ["github.com", "github.com"],
  pgroup        => [20603, 20604],
  pid           => [20603, 20604],
  ppid          => [20600, 20500],
  program       => ["upload-pack", "upload-pack"],
}

【讨论】:

  • OP 不太可能理解这段代码,因此他们不会从中学到任何东西。 :\
【解决方案2】:

我只会使用数组的散列:

#!/usr/bin/env perl 
use strict;
my %hash;
## read input line by line into $_
while (<>) {
    ## remove trailing newlines
    chomp;
    ## skip empty lines
    next if /^\s*$/;
    ## split the line on `:` into the @F array
    my @F=split(/:\s*/);
    ## Add this value to the list associated with 
    ## this key.
    push @{$hash{$F[0]}},$F[1];
}
## Set the output field separator to a comma
$"=",";
## Print each key and the array of its values separated
## by a comma.
print "$_ : ", join(",",@{$hash{$_}}),"\n" for keys(%hash)

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多