use warnings; use strict; #my $testout=`$test list`; my $testout=' CoID: Type: State: ID: ExampleCo A former 97546 ExampleCo B pending 48541 ExampleCo A ready 75521 ExampleCo B former 50123 ExampleCo A contact 60047 ExampleCo B contact 19425 '; my @order; my $array={}; my @sparse; my @compact; for my $l (split("\n",$testout)) { chomp $l; my ($coid,$type,$state,$id)=split(' ',$l,4); next unless ($type eq 'A'); my $h={ID=>$id,CoID=>$coid,State=>$state}; push @order,$id; $array->{$id}=$h; $sparse[$id]=$h; push @compact,$h; } print 'From hash'."\n"; for my $o (@order) { my $h=$array->{$o}; print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; } print 'iterate compact'."\n"; for my $h (@compact) { next unless (defined $h); print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; } print 'index sparse'."\n"; for my $o (@order) { my $h=$sparse[$o]; print ' ID = '.$h->{ID}."\n"; print ' CoID = '.$h->{CoID}."\n"; print ' State = '.$h->{State}."\n"; }