【问题标题】:How to parse a text file having data in particular format and store it in hash using perl如何解析具有特定格式数据的文本文件并使用 perl 将其存储在哈希中
【发布时间】:2012-06-05 03:58:50
【问题描述】:

我有一个包含以下格式数据的文本文件

%app_lookup_strings = (
                      common => {
                                  password => "password: ",
                                  select_action => "Select action => ",
                                  mgt_close => "BSA_MgtClose",
                                },

我必须从文本文件中读取这些数据并将其存储回哈希中。我需要知道如何解析具有此类数据的文本文件,并以与上所示类似的哈希方式存储它。

【问题讨论】:

  • 使用Storable ;并存储store \%app_lookup_strings, 'file.txt'; 以取回my $ref = retrieve('file.txt');
  • 相关:Store and read hash and array in files,这个问题比这里的这个问题更笼统。

标签: perl hash text-parsing


【解决方案1】:

如果您的文本文件只包含一个哈希,最直接的解决方案是:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my %hash = do 'text.file';

print Dumper \%hash;

输出:

$VAR1 = {
          'common' => {
                        'mgt_close' => 'BSA_MgtClose',
                        'password' => 'password: ',
                        'select_action' => 'Select action => '
                      }
        };

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 2018-12-29
    • 2013-03-06
    • 1970-01-01
    • 2020-09-07
    • 2016-04-20
    • 1970-01-01
    • 2018-07-04
    • 2021-02-27
    相关资源
    最近更新 更多