Hello Perlmonks,

Thank you again for all the advice and help you've given me on my assignment. I truly appreciated it.

What I'm placing below is the answer that worked for me, and my system. It might still not be all that pretty, but it worked.

*IF* the reader of this post is doing this same assignment and needed this help, I STRONGLY SUGGEST TRYING TO UNDERSTAND IT BEFORE COPYING IT. YOUR NEXT HOMEWORK ASSIGNMENT WILL NOT BE ONLINE FOR YOU, AND IT WON'T HELP YOU TO LEARN TO CODE.

That is all. THANK YOU!!!!

$, = " \n";

$, is an output field separator "If defined, this value is printed between each of # print's arguments." (http://perldoc.perl.org/perlvar.html#The-Syntax-of-#Variable-Names). It is equal to a new line ( "\n"). Upon review I don't think this #was necessary.

open (FH, $ARGV[0]) or die ("could not open file"); my @data = <FH>;

The open command was used to open a file. ARGV is a array file in Perl that needs to be declared before you open the program (i.e. perl homework.pl data.txt) FH is the file handle (or name) given to the ARGV array. I converted FH to my @data on the next line.

foreach my $data (@data) { $data =~ m/(\w+:-?\d+)/; my @numbers= split(/:/,$1); my $word = $numbers[0]; my $value = $numbers[1]; $myhash{$word} = $myhash{$word} + $value; }

"The foreach loop repeats over a normal list value and sets the variable VAR to #be each element of the list in turn. If the variable is preceded with the keyword #my, then it is lexically scoped, and is therefore visible only within the loop."

(http://perldoc.perl.org/perlsyn.html#Compound-Statements)

This foreach loop is reading the @data scalar and matching any word from this pattern matcher:

m/(\w+:-?\d+)/;

m - specified that string has new line

\w - a word character a-z

+ = match one or more of previous word

:-? = i am not clear on this unfortunately

\d = any digits 0-9

+ = match one or more of previous number.

The array's name is changed to my @numbers and we're splitting the : out of the array. my $word is a empty scalar, and my $value is any number that was a previous value from the array. if the words match the numbers will be added #together and placed in the empty hash table. The hash table is like a legend, to read more about them look here (http://www.tutorialspoint.com/perl/perl_hashes.htm)

while(my($key, $value) = each %myhash){ print $key . ":" . $value . "\n"; }

this while loop goes through the hash and places a colon in between the new $key value and new $value so when the program prints the results is looks the same as the original.

close (FH);
this closes the file you opened.

I placed numerous links above because I might not be the best at explaining, and these websites are fantastic. Read the comments I've gotten and try your best to continue learning. I'm finding a book by o'reilly called 'Perl in a nutshell' to also be incredibly helpful.

link to 'Perl in a nutshell': http://www.amazon.com/Perl-Nutshell-Desktop-Quick-Reference/dp/0596002416

THANK YOU (and good luck for future perlies) <3

In reply to Re: A Beginner Needs Homework help by Swizzlestix617
in thread 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.