Solved

making a different data structure

Posted on 2009-05-07
6
142 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
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…

756 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