我正在將 CSV 檔案讀入多級哈希。我正在嘗試遍歷哈希。
這是加載哈希的代碼。有用。如果我使用 Dumper 列印它,我就會得到我所期望的。
use Data::Dumper;
my $connectionsinfile = "switchconnections.csv";
my %switchportinfo;
open (my $INFILE,'<',$connectionsinfile) or die "Couldn't open SVC input file $connectionsinfile\n$!";
my @recs = <$INFILE>;
close $INFILE;
chomp @recs;
use constant { true => 1, false => 0};
my @header = split',', $recs[0];
# now that we have the header, remove it from the array
shift @recs;
foreach my $rec (@recs) {
my @data = split(',',$rec);
@row_data{@header} = @data;
my $switchname = $row_data{'Switch Name'};
my $switchport = $row_data{'Port'};
$switchportinfo{$switchname}{$switchport} = {%row_data};
}
此代碼有效,除了它列印外部和內部鍵,然后列印內部鍵的值 'HASH(XXXXXX)'
for $key (keys %switchportinfo)
{
print "$key: \n";
for $ele (keys %{$switchportinfo{$key}})
{
print " $ele: " . $switchportinfo{$key}->{$ele} . "\n";
}
}
此代碼導致錯誤“在 test2.pl 中,鍵的 arg 1 型別必須是散列或陣列(不是鍵/值散列切片)”}
我希望能夠列印內部哈希值。
for $key (keys %switchportinfo)
{
print "$key: \n";
for $ele (keys %{$switchportinfo{$key}})
{
print "$ele: \n";
for $ele2 (keys %{switchportinfo{$key}{$ele}}) {
print " $ele2: " . $switchportinfo{$key}{$ele}->{$ele2}. "\n";
}
}
}
uj5u.com熱心網友回復:
你缺少一個$in %{switchportinfo{$key}{$ele}}。始終使用use strict; use warnings;.
最后一個片段是正確的。但是讓我們使用更好的名稱,例如第一個片段中使用的名稱。
for my $switchname (keys %switchportinfo)
{
print "switchname: $switchname\n";
for my $switchport (keys %{ $switchportinfo{$switchname} })
{
print " switchport: $switchport\n";
for my $header (keys %{ $switchportinfo{$switchname}{$switchport} }) {
print " $header: $switchportinfo{$switchname}{$switchport}{$header}\n";
}
}
}
uj5u.com熱心網友回復:
以下示例代碼演示了處理輸入資料和輸出收集記錄的可能場景之一。
注意:未提供輸入檔案格式,示例代碼基于可能的輸入格式假設
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $debug = 0;
my($header,@header,@recs,%sw_info);
$header = <DATA>;
@recs = <DATA>;
@header = split(',',$header);
chomp @header;
chomp @recs;
foreach my $rec (@recs) {
my %sw;
@sw{@header} = split(',',$rec);
$sw_info{$sw{name}}{$sw{port}} = \%sw;
}
say Dumper(\%sw_info) if $debug;
for my $sw ( sort keys %sw_info ) {
say 'Switch: ' . $sw;
say "=" x 45;
for my $port ( keys %{$sw_info{$sw}} )
{
my $port_info = $sw_info{$sw}{$port};
say "\t$_ => $port_info->{$_} " for sort keys %$port_info;
say "\t" . '-' x 25;
}
}
__DATA__
r #,name,port,ip,location
1,sw1,12,192.168.0.1,office 35/north side
2,sw4,8,192.168.1.15,office 31/west side
3,sw1,24,192.168.0.13,office 41/south side
4,sw1,15,192.168.0.11,office 23/south side
輸出
Switch: sw1
=============================================
ip => 192.168.0.11
location => office 23/south side
name => sw1
port => 15
r # => 4
-------------------------
ip => 192.168.0.13
location => office 41/south side
name => sw1
port => 24
r # => 3
-------------------------
ip => 192.168.0.1
location => office 35/north side
name => sw1
port => 12
r # => 1
-------------------------
Switch: sw4
=============================================
ip => 192.168.1.15
location => office 31/west side
name => sw4
port => 8
r # => 2
-------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316243.html
標籤:perl
下一篇:Perl評估范圍
