Solved

How to create an associative array at runtime in perl?

Posted on 2006-11-03
9
477 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
Comment Utility
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
Comment Utility
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
Comment Utility
my %data;
while( <> ){
   split;
   $data{$_[0]}+=$_[1];
}
print "$_->$data{$_}, " for sort keys %data;
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now