in reply to A Beginner Needs Homework help
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.
this closes the file you opened.close (FH);
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A Beginner Needs Homework help
by GrandFather (Saint) on Apr 14, 2014 at 03:31 UTC |