Solved

How to create an associative array at runtime in perl?

Posted on 2006-11-03
9
487 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

21 Experts available now in Live!

Get 1:1 Help Now