Solved

How to create an associative array at runtime in perl?

Posted on 2006-11-03
9
518 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
[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
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
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: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Title # Comments Views Activity
iSeries PERL Scripts 7 207
Awk Question 2 136
Formatting stings with pack and printf in perl 5 84
Merging two files with Perl 5 57
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

739 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