Swizzlestix617 has asked for the wisdom of the Perl Monks concerning the following question:
Hi. My name's Ang and I'm new (to perlmonks and to Perl, first computer programming class) and I need help with a homework assignment. This is my homework assignment:
Consider a file consisting of lines that contain a color and an amount separated by colons, like:
red:27
yellow:102
green:311
yellow:12
blue:45
Should output (not necessarily in that order):red:27
yellow:114
green:311
blue:45
Your program must take the input file as a parameter.
This is the code I've written:#!/usr/bin/perl; use strict; use warnings; open (FH,$ARGV[0]) or print "could not open file" ; my $info = <FH>; while ($info=~ /(\w+ (-?\d+ )+)/) { my $info =~ s/(\w+ (-?\d+ )+)//; } my @nums = split(/:/,$info); my $word = shift (@nums); my $sum = 0; my $var = ($info); foreach $var (@nums) { $sum = $sum + $var; } my %hash = ($word = $sum); print @nums; close(FH);
These are the errors I'm getting.. and I don't understand them, how to fix them.. I feel like I'm not that good at this, so if you decide to help me explain it like I'm a kindergardener..
Argument "27 yellow" isn't numeric in addition (+) at ar991homework411 line 21, <FH> line 1. Argument "102 green" isn't numeric in addition (+) at ar991homework411 line 21, <FH> line 1. Argument "311 yellow" isn't numeric in addition (+) at ar991homework411 line 21, <FH> line 1. Argument "12 blue" isn't numeric in addition (+) at ar991homework411 line 21, <FH> line 1. Odd number of elements in hash assignment at ar991homework411 line 25, <FH> line 1.
Thanks for any help! Ang
|
|---|