List into hash

R

Randy

I am trying to take a list of domains and put them into a listing and
keep track of how many are repeated. I thought a hash would be the
easiest way to do this. I was wondering if anyone had any advice on
how to take the list I have, put each item in the list into the hash
without putting one in more then once, and then if it does show up
more then once, to add the value of the key of that item by one?

Sorry if this sounds confusing, I know what I'm trying to do, it's
just hard to explain it.

Thanks!
 
B

Ben Morrow

I am trying to take a list of domains and put them into a listing and
keep track of how many are repeated. I thought a hash would be the
easiest way to do this. I was wondering if anyone had any advice on
how to take the list I have, put each item in the list into the hash
without putting one in more then once, and then if it does show up
more then once, to add the value of the key of that item by one?

my %domains;
my @domains = get_domain_list;

$domains{$_}++ for @domains;

print "domain $_: $domains{$_} times\n" for keys %domains;

Ben
 
T

Tore Aursand

I am trying to take a list of domains and put them into a listing and
keep track of how many are repeated.

my %domains = ();
foreach ( @domain ) { # @domain is defined elsewhere
$domains{$_}++;
}
 
B

Brad Baxter

my %domains = ();
foreach ( @domain ) { # @domain is defined elsewhere
$domains{$_}++;
}

perl -wlpe '$_{$_}++}{$_="@{[%_]}"' domainlist

or perhaps

perl -walpe 'map{$_++}@_{@F}}{$_="@{[%_]}"' domainlist

Regards,

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top