Solved

making a different data structure

Posted on 2009-05-07
6
144 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:cucugirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 

Author Comment

by:cucugirl
ID: 24327973
my bad... i currently have this:

my %data  =  {'15'=>['MATH','412'],'16'=>['Thermo', '235'],'17'=>['statics','211'], '18'=>['design','250']};
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 24328008
my %data = (map{$couner++=>$_}@table);
0
 
LVL 84

Expert Comment

by:ozo
ID: 24328048
['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']
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 24328156
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24328190
@{$data{$counter}} will autovivify $data{$counter} as an array
did you mean
$data{$counter}={%$check}
instead of
push(@{$data{$counter}},%$check)
0
 
LVL 84

Expert Comment

by:ozo
ID: 24328222
What is the purpose of having a hash with sequential numeric keys instead of an array?
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question