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


In reply to A Beginner Needs Homework help by Swizzlestix617

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.