如何仅使用 Data::Dumper 输出中的一个键和值

发布于 2024-12-05 03:35:08 字数 1462 浏览 1 评论 0原文

我有数据转储器将远程托管的 xml 文件输出到本地文本文件中,并且我得到以下信息:

$VAR1 = {
    'resource' => {
        '005cd410-41d6-4e3a-a55f-c38732b73a24.xml' => {
            'standard' => 'DITA',
            'area' => 'holding',
            'id' => 'Comp_UKCLRONLINE_UKCLR_2000UKCLR0278',
        },
        '003c2a5e-4af3-4e70-bf8b-382d0b4edda1.xml' => {
            'standard' => 'DITA',
            'area' => 'holding',
            'id' => 'Comp_UKCLRONLINE_UKCLR_2000UKCLR0278',
        },  

等。我想要做的是仅使用每个资源中的一个/键和值。即选择 ID,然后从中创建一个 url。

我通常会在文件上使用正则表达式并从中提取我需要的信息,但我认为必须有一种更简单/正确的方法,但无法想出在搜索中使用的正确术语,因此找不到它。

这是我用来将此输出写入文件的代码:

#-----------------------------------------------
sub request_url {
#-----------------------------------------------
my $useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
$resource = $useragent->request( $request );                                            
}


#-----------------------------------------------
sub file_write {
#-----------------------------------------------
open OUT, ">$OUT" or Log_message ("\n$DATE - $TIME - Could not create filelist.doc \t");
Log_message ("\n$DATE - $TIME - Opened the output file");
print OUT Dumper (XML::Simple->new()->XMLin( $resource->content ));
Log_message ("\n$DATE - $TIME - Written the output file");
}

谢谢

I have data dumper outputting a remotely hosted xml file into a local text file and I am getting the following info:

$VAR1 = {
    'resource' => {
        '005cd410-41d6-4e3a-a55f-c38732b73a24.xml' => {
            'standard' => 'DITA',
            'area' => 'holding',
            'id' => 'Comp_UKCLRONLINE_UKCLR_2000UKCLR0278',
        },
        '003c2a5e-4af3-4e70-bf8b-382d0b4edda1.xml' => {
            'standard' => 'DITA',
            'area' => 'holding',
            'id' => 'Comp_UKCLRONLINE_UKCLR_2000UKCLR0278',
        },  

etc. What I want to do is work with just one/key and value in each resource. Ie pick out the ID and then create a url from that.

I would normally use a regex on the file and pull the info I need from that but I'm thinking there must be an easier/proper way but can't think of the right term to use in a search and am therefore not finding it.

Here is the code I am using to write this output to a file:

#-----------------------------------------------
sub request_url {
#-----------------------------------------------
my $useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
$resource = $useragent->request( $request );                                            
}


#-----------------------------------------------
sub file_write {
#-----------------------------------------------
open OUT, ">$OUT" or Log_message ("\n$DATE - $TIME - Could not create filelist.doc \t");
Log_message ("\n$DATE - $TIME - Opened the output file");
print OUT Dumper (XML::Simple->new()->XMLin( $resource->content ));
Log_message ("\n$DATE - $TIME - Written the output file");
}

thanks

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

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

发布评论

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

评论(2

陌上青苔 2024-12-12 03:35:08

我不太明白你的问题,但我猜你想从哈希中访问一些数据。

你不需要正则表达式或其他东西;只需“执行”数据并从返回的 hassref 中获取值:

以一个简单的单行为例(假设您的文件名为“dumper.out”):

perl -Mstrict -wE 'my $hashref = do{ do "dumper.out" }; say $hashref->{resource}{"005cd410-41d6-4e3a-a55f-c38732b73a24.xml"}{id}'

HTH,Paul

I'm not really understanding your question, but I'm guessing you want to access some data from the hash.

You don't need a regex or other strage stuff; just `do` your data and get the value from the hassref you get back:

A simple one liner as an example (assuming your file is called `dumper.out`):

perl -Mstrict -wE 'my $hashref = do{ do "dumper.out" }; say $hashref->{resource}{"005cd410-41d6-4e3a-a55f-c38732b73a24.xml"}{id}'

HTH, Paul

卸妝后依然美 2024-12-12 03:35:08

也许您想遍历由 XML::Simple 构建的数据结构。
每个资源都位于使用 resource 键和 $doc 数据结构获得的 ARRAYREF 中。

use XML::Simple;
use LWP;
use Data::Dumper;

my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
my $res = $ua->request( $req );

my $xs         = XML::Simple->new();
my $doc        = $xs->XMLin( $res->content );

printf "resources: %s\n", scalar keys %{ $doc->{ resource } };

foreach ( keys %{ $doc->{ resource } } ) {
    printf "resource => %s, id => %s\n", $_, $doc->{ resource }->{ $_ }->{ id };
}

输出是这样的:

resources: 7
resource => 005cd410-41d6-4e3a-a55f-c38732b73a24.xml, id => Comp_UKCLRONLINE_UKCLR_2000UKCLR0278
resource => 003c2a5e-4af3-4e70-bf8b-382d0b4edda1.xml, id => Comp_UKCLRONLINE_UKCLR_2002UKCLR0059
resource => 0033d4d3-c397-471f-8cf5-16fb588b0951.xml, id => Comp_UKCLRONLINE_UKCLR_navParentTopic_67
resource => 002a770a-db47-41ef-a8bb-0c8aa45a8de5.xml, id => Comp_UKCLRONLINE_UKCLR_navParentTopic_308
resource => 000fff79-45b8-4ac3-8a57-def971790f16.xml, id => Comp_UKCLRONLINE_UKCLR_2002UKCLR0502
resource => 00493372-c090-4734-9a50-8f5a06489591.xml, id => Comp_UKCLRONLINE_COMPCS_2010_10_0002
resource => 004377bf-8e24-4a69-9411-7c6baca80b87.xml, id => Comp_CLJONLINE_CLJ_2002_01_11

Maybe you want to walk the data structure built by XML::Simple.
Each resource is inside an ARRAYREF you get using the resource key with $doc data structure.

use XML::Simple;
use LWP;
use Data::Dumper;

my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
my $res = $ua->request( $req );

my $xs         = XML::Simple->new();
my $doc        = $xs->XMLin( $res->content );

printf "resources: %s\n", scalar keys %{ $doc->{ resource } };

foreach ( keys %{ $doc->{ resource } } ) {
    printf "resource => %s, id => %s\n", $_, $doc->{ resource }->{ $_ }->{ id };
}

The output is this:

resources: 7
resource => 005cd410-41d6-4e3a-a55f-c38732b73a24.xml, id => Comp_UKCLRONLINE_UKCLR_2000UKCLR0278
resource => 003c2a5e-4af3-4e70-bf8b-382d0b4edda1.xml, id => Comp_UKCLRONLINE_UKCLR_2002UKCLR0059
resource => 0033d4d3-c397-471f-8cf5-16fb588b0951.xml, id => Comp_UKCLRONLINE_UKCLR_navParentTopic_67
resource => 002a770a-db47-41ef-a8bb-0c8aa45a8de5.xml, id => Comp_UKCLRONLINE_UKCLR_navParentTopic_308
resource => 000fff79-45b8-4ac3-8a57-def971790f16.xml, id => Comp_UKCLRONLINE_UKCLR_2002UKCLR0502
resource => 00493372-c090-4734-9a50-8f5a06489591.xml, id => Comp_UKCLRONLINE_COMPCS_2010_10_0002
resource => 004377bf-8e24-4a69-9411-7c6baca80b87.xml, id => Comp_CLJONLINE_CLJ_2002_01_11
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文