Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: confused with summing certain elements from an array

by Rudif (Hermit)
on Nov 20, 2007 at 22:11 UTC ( #652006=note: print w/replies, xml ) Need Help??


in reply to confused with summing certain elements from an array

... but I seem to be unable to begin...

G. Polya would perhaps suggest "Solve a simpler problem first".

#!/usr/bin/perl use warnings; use strict; #Hello all, I have a school assignment but I seem to be unable to begi +n... What I was given is something like the following my @out= split ',', "b,a,c,d,a,b,f,g,a,c,g"; # look no warning my @out1=(1,2,3,1,2,3,4,5,2,3,2);#the numbers declares how many times ++the letters from the @out are found #I want to make a hash contains as keys the unique elements from the @ +out and as values the sums of the letters from the @out1. Also I want + the hash to be sorted #This is the preview of the hash my %hash=(a=>6,b=>4,c=>6,d=>1,f=>4,g=>7); #any ideas please? ### Remember to use warnings and strict (put it on the first lines of +your code): # ok ### You can iterate the keys of the hash in order as follows: print "preview:\n"; print "$_: \t$hash{$_}\n" for sort keys %hash; # ok, let me try a simpler problem first my %counts; for (@out) { $counts{$_}++; } print "simpler:\n"; print "$_: \t$counts{$_}\n" for sort keys %counts; # that worked, let me try the full assignment my %counts2; for (@out) { my $count = shift @out1; $counts2{$_} += $count; } print "full:\n"; print "$_: \t$counts2{$_}\n" for sort keys %counts2; # this looks about right.

Rudif

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://652006]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2023-01-31 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?