Link to home
Start Free TrialLog in
Avatar of cucugirl
cucugirl

asked on

making a different data structure

one more question.. I want to make an HoH from an array but don't know why when I use print Dumper it does not look like a HoH it looks more like HoA i would say... attached is what I got...

i want something like this:
my %data  =  {'15'=>{'MATH'=>'412'},'16'=>{'Thermo'=> '235'},'17'=>{'statics'=>'211'}, '18'=>{'design'=>'250'}}; I currently have this:
my %data  =  {'15'=>['MATH'=>'412'],'16'=>['Thermo'=> '235'],'17'=>['statics'=>'211'], '18'=>['design'=>'250']};


my $counter = $#newper+1;
   
   foreach my $check (sort @table){
      
      push(@{$data{$counter}},%$check);
   
   $counter++;
   
   }
 
my @table = ({'MATH'=>'412'},{'Thermo'=> '235'},{'statics'=>'211'}, {'design'=>'250'});

Open in new window

Avatar of cucugirl
cucugirl

ASKER

my bad... i currently have this:

my %data  =  {'15'=>['MATH','412'],'16'=>['Thermo', '235'],'17'=>['statics','211'], '18'=>['design','250']};
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
['MATH'=>'412']
is the same as
['MATH','412']

=> is like , except that it automatically treats the left side as a bareword so you don;t get a warning fot
[math=>'412']
since @table contains references, sort @table will sort by the stringified references, which would come out in order of its address in memory, which is not very meaningful, ad probably not what you wanted.
@{$data{$counter}} will autovivify $data{$counter} as an array
did you mean
$data{$counter}={%$check}
instead of
push(@{$data{$counter}},%$check)
What is the purpose of having a hash with sequential numeric keys instead of an array?