2

我是 perl 的新手,我试图将一些数据插入哈希中,但卡住了,不胜感激....

我有以下数据

Catalog database partition number          = 0
Snapshot timestamp                         = 02/23/2013 21:02:08.262661
Number of automatic storage paths          = 1
      File system ID                       = 2304
      Storage path free space (bytes)      = 1385955328
      File system used space (bytes)       = 26495475712
      File system total space (bytes)      = 27948539904
High water mark for connections            = 82
Application connects                       = 32747
Secondary connects total                   = 3
Applications connected currently           = 22
Appls. executing in db manager currently   = 1
Agents associated with applications        = 24
Maximum coordinating agents                = 82
Locks held currently                       = 6
Lock waits                                 = 172
Lock list memory in use (Bytes)            = 33264
Deadlocks detected                         = 2
Lock escalations                           = 0
Exclusive lock escalations                 = 0
Agents currently waiting on locks          = 0
Lock Timeouts                              = 87
Number of indoubt transactions             = 0
Total Private Sort heap allocated          = 0
Total Shared Sort heap allocated           = 45
Shared Sort heap high water mark           = 36412
Total sorts                                = 44986014
Sort overflows                             = 4928
Active sorts                               = 1
Buffer pool data logical reads             = 137473219
Buffer pool data physical reads            = 6403328
Buffer pool temporary data logical reads   = 9360028
Buffer pool temporary data physical reads  = 7684
Asynchronous pool data page reads          = 6031755
Buffer pool data writes                    = 1477714
Asynchronous pool data page writes         = 6667
Buffer pool index logical reads            = 7386632979
Buffer pool index physical reads           = 418928
Buffer pool temporary index logical reads  = 0
Buffer pool temporary index physical reads = 0
Asynchronous pool index page reads         = 26653
Buffer pool index writes                   = 127741
Asynchronous pool index page writes        = 103030
Buffer pool xda logical reads              = 4
Buffer pool xda physical reads             = 4
Buffer pool temporary xda logical reads    = 0
Buffer pool temporary xda physical reads   = 0
Buffer pool xda writes                     = 0
Asynchronous pool xda page reads           = 0
Asynchronous pool xda page writes          = 0
Total buffer pool read time (milliseconds) = 39674360
Total elapsed asynchronous read time       = 33908937
Total elapsed asynchronous write time      = 1806167
Asynchronous data read requests            = 5770802
Asynchronous index read requests           = 2997
Asynchronous xda read requests             = 0
No victim buffers available                = 6679989
LSN Gap cleaner triggers                   = 162
Dirty page steal cleaner triggers          = 1150
Dirty page threshold cleaner triggers      = 118
Time waited for prefetch (ms)              = 18934507
Unread prefetch pages                      = 2147
Direct reads                               = 126622020
Direct writes                              = 43430
Direct read requests                       = 228012
Direct write requests                      = 3441
Direct reads elapsed time (ms)             = 1732753
Direct write elapsed time (ms)             = 12100
Database files closed                      = 0
Commit statements attempted                = 118401
Rollback statements attempted              = 1128
Dynamic statements attempted               = 352842
Static statements attempted                = 36096602329
Failed statement operations                = 26388
Select SQL statements executed             = 21659801876
Xquery statements executed                 = 0
Update/Insert/Delete statements executed   = 549460
DDL statements executed                    = 906
Inactive stmt history memory usage (bytes) = 0
Internal automatic rebinds                 = 42
Internal rows deleted                      = 9428
Internal rows inserted                     = 44
Internal rows updated                      = 117
Internal commits                           = 39840
Internal rollbacks                         = 104
Internal rollbacks due to deadlock         = 17
Rows deleted                               = 6225
Rows inserted                              = 295031
Rows updated                               = 12347
Rows selected                              = 14447342482
Rows read                                  = 656654938
Binds/precompiles attempted                = 27
Log space used by the database (Bytes)     = 3750726
Maximum secondary log space used (Bytes)   = 0
Maximum total log space used (Bytes)       = 40319972
Secondary logs allocated currently         = 0
Log pages read                             = 5561
Log read time (sec.ns)                     = 5.000000004
Log pages written                          = 89962
Log write time (sec.ns)                    = 3046.000000004
Number write log IOs                       = 74822
Number read log IOs                        = 696
Number partial page log IOs                = 22821
Number log buffer full                     = 0
Log data found in buffer                   = 152701
Appl id holding the oldest transaction     = 11
Log to be redone for recovery (Bytes)      = 3765430
Log accounted for by dirty pages (Bytes)   = 3765430
File number of first active log            = 3691
File number of last active log             = 3740
File number of current active log          = 3691
Package cache lookups                      = 185429597
Package cache inserts                      = 9486
Package cache overflows                    = 0
Package cache high water mark (Bytes)      = 52494336
Application section lookups                = 1737096759
Application section inserts                = 173693
Catalog cache lookups                      = 737785
Catalog cache inserts                      = 960
Catalog cache overflows                    = 0
Catalog cache high water mark              = 4521984
 Shared high water mark                    = 0
 Corresponding shared overflows            = 0
 Total shared section inserts              = 0
 Total shared section lookups              = 0
 Private high water mark                   = 13232700
 Corresponding private overflows           = 0
 Total private section inserts             = 173693
 Total private section lookups             = 2927458754
Number of hash joins                       = 1418296
Number of hash loops                       = 0
Number of hash join overflows              = 15
Number of small hash join overflows        = 0
Post threshold hash joins (shared memory)  = 0
Active hash joins                          = 0
       Current size (bytes)                    = 655360
       High water mark (bytes)                 = 7733248
       Configured size (bytes)                 = 20512768
       Current size (bytes)                    = 47710208
       High water mark (bytes)                 = 52494336
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 4521984
       High water mark (bytes)                 = 4521984
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 6
       Current size (bytes)                    = 15073280
       High water mark (bytes)                 = 15073280
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 5
       Current size (bytes)                    = 69402624
       High water mark (bytes)                 = 69402624
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 4
       Current size (bytes)                    = 41549824
       High water mark (bytes)                 = 41549824
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 3
       Current size (bytes)                    = 263454720
       High water mark (bytes)                 = 263454720
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 2
       Current size (bytes)                    = 8585216
       High water mark (bytes)                 = 8585216
       Configured size (bytes)                 = 1059061760
       Secondary ID                            = 1
       Current size (bytes)                    = 169279488
       High water mark (bytes)                 = 169279488
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 851968
       High water mark (bytes)                 = 851968
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 589824
       High water mark (bytes)                 = 589824
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 458752
       High water mark (bytes)                 = 458752
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 393216
       High water mark (bytes)                 = 393216
       Configured size (bytes)                 = 1059061760
       Current size (bytes)                    = 2490368
       High water mark (bytes)                 = 18284544
       Configured size (bytes)                 = 146800640
       Current size (bytes)                    = 212926464
       High water mark (bytes)                 = 212926464
       Configured size (bytes)                 = 212992000
       Current size (bytes)                    = 15073280
       High water mark (bytes)                 = 25690112
       Configured size (bytes)                 = 105381888
       Current size (bytes)                    = 131072
       High water mark (bytes)                 = 131072
       Configured size (bytes)                 = 20971520

现在我如何将它插入哈希

我做的方式是

%hash = map {chomp ; split /\s+=\s+[0-9]/} (@dblst);

其中 dblst 是包含数据的数组

当我这样做时,我得到

$VAR1 = {
          '' => '0971520',
          '385955328' => '      File system used space (bytes)',
          'Deadlocks ' => 'detected',
          'triggers' => '18',
          'Catalog cache ' => 'overflows',
          '69279488' => '       Configured size (bytes)',
          'Catalog cache high water ' => 'mark',
          'Internal rows ' => 'updated',
          'Failed statement ' => 'operations',
          'Snapshot ' => 'timestamp',
          '733248' => '       Configured size (bytes)',
          'Rows ' => 'read',
          'written' => '9962',
          'Buffer pool temporary data physical ' => 'reads',
          'reads' => 'Asynchronous pool xda page ',
          'time' => '806167',
          'Number of small hash join ' => 'overflows',
          '31072' => '       Configured size (bytes)',
          '750726' => 'Maximum secondary log space used (Bytes)',
          'Asynchronous pool index page ' => 'writes',
          'Direct ' => 'reads',
          '      File system total space (bytes)' => '',
          'lookups' => '926693856',
          'transactions' => 'Total Private Sort heap ',
          'Log space used by the database (Bytes)' => '',
          'sorts' => 'Buffer pool data logical ',
          '490368' => '       High water mark (bytes)',
          'Buffer pool temporary index physical ' => 'reads',
          '.000000004' => 'Log pages ',
          'inserted' => '95031',
          '684' => 'Asynchronous pool data page ',
          '28012' => 'Direct write ',
          '691' => 'Package cache ',
          'Time waited for prefetch (ms)' => '',
          'rebinds' => '2',
          '059061760' => '       Current size (bytes)',
          'connections' => '2',
          'Database files ' => 'closed',
          '       Current size (bytes)' => '',
          'Log write time (sec.ns)' => '',
          '17' => 'Internal ',
          '7948539904' => 'High water mark for ',
          '7710208' => '       High water mark (bytes)',
          'transaction' => '1',
          '51968' => '       High water mark (bytes)',
          'Log read time (sec.ns)' => '',
          'Update/Insert/Delete statements ' => 'executed',
          'Shared Sort heap high water ' => 'mark',
          'total' => 'Applications connected ',
          '3908937' => 'Total elapsed asynchronous write ',
          '6653' => 'Buffer pool index ',
          'Asynchronous xda read ' => 'requests',
          'joins' => '       Current size (bytes)',
          'Inactive stmt history memory usage (bytes)' => 'Internal automatic ',
          'Commit statements ' => 'attempted',
          '      Storage path free space (bytes)' => '',
          ' Total private section ' => 'inserts',
          'ID' => '       Current size (bytes)',
          'Agents associated with ' => 'applications',
          '765430' => 'Log accounted for by dirty pages (Bytes)',
          'Agents currently waiting on ' => 'locks',
          'Buffer pool temporary index logical ' => 'reads',
          '6412' => 'Total ',
          '1549824' => '       High water mark (bytes)',
          '       Configured size (bytes)' => '',
          '8934507' => 'Unread prefetch ',
          '96' => 'Number partial page log ',
          '585216' => '       High water mark (bytes)',
          '486' => 'Package cache ',
          'Log to be redone for recovery (Bytes)' => '',
          'commits' => '9840',
          '05381888' => '       Current size (bytes)',
          '93216' => '       Configured size (bytes)',
          '46800640' => '       Current size (bytes)',
          'writes' => '3430',
          'Exclusive lock ' => 'escalations',
          'No victim buffers ' => 'available',
          '428' => 'Internal rows ',
          '7' => 'Number of indoubt ',
          'loops' => 'Number of hash join ',
          'mark' => '3232700',
          '52842' => 'Static statements ',
          'Maximum total log space used (Bytes)' => '',
          '72' => 'Lock list memory in use (Bytes)',
          'Buffer pool data ' => 'writes',
          'Total elapsed asynchronous read ' => 'time',
          'currently' => 'Log pages ',
          'Application ' => 'connects',
          'File number of first active ' => 'log',
          'Dirty page steal cleaner ' => 'triggers',
          'agents' => '2',
          'allocated' => '5',
          'Xquery statements ' => 'executed',
          'inserts' => ' Total shared section ',
          ' Corresponding private ' => 'overflows',
          'pages' => '147',
          'Number of hash ' => 'joins',
          '225' => 'Rows ',
          '2747' => 'Secondary connects ',
          'Buffer pool index logical ' => 'reads',
          'Dynamic statements ' => 'attempted',
          'paths' => '      File system ',
          '6388' => 'Select SQL statements ',
          'deadlock' => '7',
          '       High water mark (bytes)' => '',
          '63454720' => '       Configured size (bytes)',
          '26622020' => 'Direct ',
          'IOs' => '2821',
          'attempted' => '7',
          '0319972' => 'Secondary logs allocated ',
          'Asynchronous data read ' => 'requests',
          'executed' => '06',
          'Catalog database partition ' => 'number',
          '403328' => 'Buffer pool temporary data logical ',
          'Direct read ' => 'requests',
          'Number read log ' => 'IOs',
          '477714' => 'Asynchronous pool data page ',
          'Locks held ' => 'currently',
          '046.000000004' => 'Number write log ',
          'Application section ' => 'lookups',
          'overflows' => '5',
          '       Secondary ' => 'ID',
          '03030' => 'Buffer pool xda logical ',
          '49460' => 'DDL statements ',
          '52701' => 'Appl id holding the oldest ',
          '04' => 'Internal rollbacks due to ',
          'Log data found in ' => 'buffer',
          'read' => '561',
          '89824' => '       Configured size (bytes)',
          '732753' => 'Direct write elapsed time (ms)',
          'Package cache ' => 'inserts',
          '18401' => 'Rollback statements ',
          '928' => 'Active ',
          'Sort ' => 'overflows',
          'Appls. executing in db manager ' => 'currently',
          'Direct reads elapsed time (ms)' => '',
          '150' => 'Dirty page threshold cleaner ',
          'Post threshold hash joins (shared memory)' => 'Active hash ',
          '2347' => 'Rows ',
          '37785' => 'Catalog cache ',
          'Internal ' => 'rollbacks',
          'Number log buffer ' => 'full',
          '9402624' => '       Configured size (bytes)',
          '521984' => '       Configured size (bytes)',
          'Buffer pool data physical ' => 'reads',
          '12926464' => '       Configured size (bytes)',
          '2/23/2013 21:01:29.252390' => 'Number of automatic storage ',
          '58752' => '       High water mark (bytes)',
          '5073280' => '       High water mark (bytes)',
          '56654938' => 'Binds/precompiles ',
          'log' => '740',
          '733272267' => 'Application section ',
          'requests' => '441',
          '385867681' => 'Buffer pool index physical ',
          '73693' => ' Total private section ',
          '679989' => 'LSN Gap cleaner ',
          '4' => 'Maximum coordinating ',
          '770802' => 'Asynchronous index read ',
          'Lock ' => 'Timeouts',
          'selected' => '4445812686',
          'File number of current active ' => 'log',
          '

你看到一些数字在左边,我想要所有的数字在右边,所以我想用那个哈希执行一些算术运算......感谢帮助

4

1 回答 1

5

你的问题是你正在使用的正则表达式,以及无限分裂。由于您只寻找一个键和一个值,您应该(可能)将拆分限制为两个字段。如果您应该有一个拆分为更少或更多字段的字段,您的哈希分配将被破坏。您还应该只在分隔符上拆分,这是一个由空格包围的等号。

你的正则表达式:

/\s+=\s+[0-9]/

将在“空格后跟等号后跟空格后跟数字”拆分。数字将被删除。假设一行如foo = 0,您将得到一个结果列表"foo", ""

你需要的是:

split /\s*=\s*/, $_, 2;

这将去除等号周围的所有空格,并且最多只返回两个字段(大概是键和值)。您仍然会发现一些包含前导空格的字段,但我认为这是可以接受的。

使用map语句的缺点是它返回整个列表,并且正如您所见,该列表可能会损坏。更明智的方法是使用 for 循环:

my %hash;
for (@dblst) {
    chomp;
    my ($key, $val) = split /\s*=\s*/, $_, 2;
    $hash{$key} = $val;
}

使用此代码还可以轻松插入控制语句以防止插入空白键/值、防止重复以及执行您想要的各种其他测试。

于 2013-02-23T15:49:00.737 回答