0

我有键值对作为“语句:测试,数据”,其中“测试,数据”是哈希值。在尝试使用这些值创建散列时,perl 将值拆分为逗号。有没有一种方法可以将带逗号的字符串用作值

4

2 回答 2

5

Perl 中没有任何东西可以阻止您使用 'test,data' 作为哈希值。如果您传入的字符串字面上是“语句:测试,数据”,您可以使用此代码添加到哈希中:

my ($key, $value) = ($string =~ /(\w+):(.*)/);
next unless $key and $value;  # skip bad stuff - up to you
$hash{$key} = $value;
于 2012-10-17T09:43:33.070 回答
3

Perl 不会用逗号分割字符串,除非你告诉它这样做。

#!/usr/bin/perl

use v5.16;
use warnings;
use Data::Dump 'ddx';

my $data = "statement:test,data";
my %hash;

my ($key, $value) = split(":", $data);

$hash{$key} = $value;

ddx \%hash;

给出:

# split.pl:14: { statement => "test,data" }
于 2012-10-17T09:42:51.113 回答