bhu731 has asked for the wisdom of the Perl Monks concerning the following question:

I have a loop that spits out
lun1 active
lun2 passive
lun1 failed
lun3 other
lun1 active

I need to output this
lunactivepassivefailedother
lun12010
lun20100
lun30001
I am a perl neophyte of course.... and have tried constructions such as $list($lun}{$status}++; $list{$lun}->{$status}++; neither of which seems to work.... as Data Dumper seems to suggest that \%list is empty.

Replies are listed 'Best First'.
Re: is a hash of hashes the right thing
by toolic (Bishop) on May 11, 2011 at 17:57 UTC
    You could loop through and fill in the missing holes:
    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys=1; my %list; while (<DATA>) { my ($k, $v) = split; $list{$k}{$v}++; } for my $k (keys %list) { for my $stat (qw(active passive failed other)) { $list{$k}{$stat} = 0 unless exists $list{$k}{$stat}; } } print Dumper(\%list); =for output $VAR1 = { 'lun1' => { 'active' => 2, 'failed' => 1, 'other' => 0, 'passive' => 0 }, 'lun2' => { 'active' => 0, 'failed' => 0, 'other' => 0, 'passive' => 1 }, 'lun3' => { 'active' => 0, 'failed' => 0, 'other' => 1, 'passive' => 0 } }; =cut __DATA__ lun1 active lun2 passive lun1 failed lun3 other lun1 active
Re: is a hash of hashes the right thing
by bhu731 (Initiate) on May 11, 2011 at 18:06 UTC
    I am a fool...the syntax for the hoh is correct...alas the place where I declared the hoh was not