Solved

How to create an associative array at runtime in perl?

Posted on 2006-11-03
9
515 Views
Last Modified: 2007-12-19
Dear Expert,

When I read a data file as format as follow at runtime
AA      1
AB      1
AC      1
AA      1
AD      1
AD      1


I need to create an associative array (AA->2,  AB->1, AC->1, AD->2) at runtime,  how should I do it?

Thanks.
Hanqian
0
Comment
Question by:Hanqian
9 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 17868613
open(IN,"<filename.txt") or die "Couldn't open file: $!\n";
my %data;
while(<IN>) {
    my @f=split /\s*/;
    $data{$f[0]} += $f[1];
}
close(IN);

0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 17868702
open(IN,"<filename.txt") or die "Couldn't open file: $!\n";
my %data;
while(<IN>) {chomp; $data{(split)[0]} += $data{(split)[1]}}
close(IN) ;
0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 17871915
my %data;
while( <> ){
   split;
   $data{$_[0]}+=$_[1];
}
print "$_->$data{$_}, " for sort keys %data;
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: 18082968
It may be more fair to split the points,  all the suggestions work, I eliminated some unecesary operations, but used a deprecated feature.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 18084935
I decided against recommending a split because of what I considered to be errors in the earlier posts.

A split on /\s*/ ends up being a split into _characters_ since \s* matches a null string, so I don't think Adam314's proposal works.

Doubling up the split call, with an unneeded (but admittedly harmless) call to chomp made me decide against Manav's proposal.

I didn't notice anything in your solution that I'd consider strongly deprecated, but I wonder whether it might not be better to have used a \n instead of a comma in your print.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18086887
You're right, I forgot that the first suggestion has used * instead of +, even though it was the reason I posted.  
The superfluousness of the second suggestion prompted a minimalist reaction even though
perldoc -f split
says that use of split in scalar context is  deprecated because it clobbers your subroutine arguments.
there are no subroutine arguments here, but one might be careful about using the same solutuon in other contexts.
I used comma because comma was used in the question.
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 18088221
<quote>
Doubling up the split call, with an unneeded (but admittedly harmless) call to chomp made me decide against Manav's proposal.
</quote>
I agree. It would be fair for the points to go to Ozo.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

740 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