perl如果将string转换成array of hashes 字符串转数组
来源:百度知道 编辑:UC知道 时间:2024/05/14 16:39:04
如何将
$string = "{file => 'file1', desc => 'test}, {file => 'file12', desc => 'test2'}"
转换成
my @array_of_hashes = (
{file => 'file1', desc => 'test},
{file => 'file12', desc => 'test2'},
);
如果是 {file => 'file1', desc => 'test,descn => 'testn }, 就是hash里n对。 同时array也含有n个数组。 有没有一永安逸得方法 。 你的方法不错。不过只是针对特定的 这个string的。 麻烦你再帮帮我一下。谢谢
$string = "{file => 'file1', desc => 'test}, {file => 'file12', desc => 'test2'}"
转换成
my @array_of_hashes = (
{file => 'file1', desc => 'test},
{file => 'file12', desc => 'test2'},
);
如果是 {file => 'file1', desc => 'test,descn => 'testn }, 就是hash里n对。 同时array也含有n个数组。 有没有一永安逸得方法 。 你的方法不错。不过只是针对特定的 这个string的。 麻烦你再帮帮我一下。谢谢
#!/bin/perl -w
use strict;
my $string = "{file => 'file1', desc => 'test'},{file => 'file12', desc => 'test2'}";
my @myarr;
while( $string =~ /{(\w+)\s* =>\s*'(\w+)',\s*(\w+)\s* =>\s*'(\w+)'}/g )
{
push @myarr,{$1 => $2, $3 =>$4};
}
print $myarr[1]->{desc},"\n";
单纯的split一下不行的。
数组的元素是匿名哈希,不是字符串,我感觉还是需要提出数据来的。
用split把它切块,然后用push 赋值给数组。