从字符串构建哈希

发布于 2024-12-12 21:51:41 字数 239 浏览 2 评论 0原文

我在 perl 上有以下字符串:

my $string = xyz;1;xyz;2;a;2;b;2 

我想在该字符串之后构建一个散列,如下所示:

my @array =split /;/,$string;

$hash{xyz} =(1,2);
$hash{b}=(2);
$hahs{a}=(2);

perl 的方法是什么?

I have the below string on perl :

my $string = xyz;1;xyz;2;a;2;b;2 

i want to build a hash after for this string like below :

my @array =split /;/,$string;

$hash{xyz} =(1,2);
$hash{b}=(2);
$hahs{a}=(2);

what is the perl way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怕倦 2024-12-19 21:51:43

可能最简单(标准)的方法是 List::MoreUtils: :natatime

use List::MoreUtils qw<natatime>;
my $iter = natatime 2 => split /;/, 'xyz;1;xyz;2;a;2;b;2';
my %hash;
while ( my ( $k, $v ) = $iter->()) { 
    push @{ $hash{ $k } }, $v;
}

然而,抽象出我可能想再次做的部分......

use List::MoreUtils qw<natatime>;

sub pairs { 
    my $iter = natatime 2 => @_;
    my @pairs;
    while ( my ( $k, $v ) = $iter->()) { 
        push @pairs, [ $k, $v ];
    }
    return @pairs;
}

sub multi_hash {
    my %h;
    push @{ $h{ $_->[0] } }, $_->[1] foreach &pairs;
    return wantarray ? %h : \%h;
}

my %hash = multi_hash( split /;/, 'xyz;1;xyz;2;a;2;b;2' );

Probably the easiest (standard) way to do this is List::MoreUtils::natatime

use List::MoreUtils qw<natatime>;
my $iter = natatime 2 => split /;/, 'xyz;1;xyz;2;a;2;b;2';
my %hash;
while ( my ( $k, $v ) = $iter->()) { 
    push @{ $hash{ $k } }, $v;
}

However abstracting out the parts that I would probably want to do again...

use List::MoreUtils qw<natatime>;

sub pairs { 
    my $iter = natatime 2 => @_;
    my @pairs;
    while ( my ( $k, $v ) = $iter->()) { 
        push @pairs, [ $k, $v ];
    }
    return @pairs;
}

sub multi_hash {
    my %h;
    push @{ $h{ $_->[0] } }, $_->[1] foreach &pairs;
    return wantarray ? %h : \%h;
}

my %hash = multi_hash( split /;/, 'xyz;1;xyz;2;a;2;b;2' );
薄凉少年不暖心 2024-12-19 21:51:42
my $string = "xyz;1;xyz;2;a;2;b;2";
my %hash;
push @{$hash{$1}}, $2 while $string =~ s/^(\w+);(\d+);?//g;

实际上

push @{$hash{$1}}, $2 while $string =~ m/(\w+);(\d+);?/g;

会更好,因为这不会吃掉你原来的字符串。

my $string = "xyz;1;xyz;2;a;2;b;2";
my %hash;
push @{$hash{$1}}, $2 while $string =~ s/^(\w+);(\d+);?//g;

Actually

push @{$hash{$1}}, $2 while $string =~ m/(\w+);(\d+);?/g;

would be better, since that doesn't eat up your original string.

烟凡古楼 2024-12-19 21:51:42

假设您希望同一键的多个值成为数组引用,那么一种方法是这样的:

my @values = split /;/, $string;

my %hash;
while( @values ) { 
    my $key = shift @values;
    my $val = shift @values;

    if ( exists $hash{$key} && !ref $hash{$key} ) { 
        # upgrade to arrayref
        $hash{$key} = [ $hash{$key}, $val ];
    } elsif ( ref $hash{$key} ) { 
        push @{ $hash{$key} }, $val;
    } else { 
        $hash{$key} = $val;
    }
}

使用您的数据,这将产生如下结构

    {
      'a' => '2',
      'b' => '2',
      'xyz' => [
                 '1',
                 '2'
               ]
    };

Assuming you want the multiple values for the same key to be an array reference, then one way to do it is like this:

my @values = split /;/, $string;

my %hash;
while( @values ) { 
    my $key = shift @values;
    my $val = shift @values;

    if ( exists $hash{$key} && !ref $hash{$key} ) { 
        # upgrade to arrayref
        $hash{$key} = [ $hash{$key}, $val ];
    } elsif ( ref $hash{$key} ) { 
        push @{ $hash{$key} }, $val;
    } else { 
        $hash{$key} = $val;
    }
}

With your data, this will result in a structure like

    {
      'a' => '2',
      'b' => '2',
      'xyz' => [
                 '1',
                 '2'
               ]
    };
-黛色若梦 2024-12-19 21:51:42

Drats:你有重复的键...我想用 mapgrep 做一些事情。

这很容易理解:

my $string = "xyz;1;xyz;2;a;2;b;2";
my @array = split /;/ => $string;

my %hash;
while (@array) {
    my ($key, $value) = splice @array, 0, 2;
    $hash{$key} = [] if not exists $hash{$key};
    push @{$hash{$key}}, $value;
}

即使密钥不在字符串中,该程序也会工作。例如,即使 xyz 被其他值对分隔,以下内容也将起作用:

my $string = "xyz;1;a;2;b;2;xyz;2";

我假设 $hash{b}=(2); 意味着您想要$hash{b} 的值是对单个成员数组的引用。这是正确的吗?

Drats: You have repeating keys... I wanted to do something with map or grep.

This is fairly simple to understand:

my $string = "xyz;1;xyz;2;a;2;b;2";
my @array = split /;/ => $string;

my %hash;
while (@array) {
    my ($key, $value) = splice @array, 0, 2;
    $hash{$key} = [] if not exists $hash{$key};
    push @{$hash{$key}}, $value;
}

This program will work even if the key is not together in your string. For example, the following will work even though xyz is separated by the other value pairs:

my $string = "xyz;1;a;2;b;2;xyz;2";

I am assuming that $hash{b}=(2); means you want the value of $hash{b} to be a reference to a single member array. Is that correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文