in reply to A Beginner Needs Homework help

Could you do your best to write a line by line explanation of your program? You can add it as comments after each line

If you're willing to do that, I'd be willing to help you straighten out your mental model of computer programs

Replies are listed 'Best First'.
Re^2: A Beginner Needs Homework help
by Swizzlestix617 (Novice) on Apr 10, 2014 at 21:39 UTC
    use strict; use warnings; #^^ for personal use to see where I've messed up open (FH,$ARGV[0]) || print "could not open file" ; my $info = <FH>; # ^^This opens whatever file is associates with the program # i.e - perl thisassignment documenthechooses # that file is placed into $info after it's read while ($info=~ /(\w+ (-?\d+ )+)/) { my $info =~ s/(\w+ (-?\d+ )+)//; } # Identifies the pattern in file. Identifies one or more words (\w+), +one or more #numbers(\d+),, also searches for new lines (-?) . my pro +fessor gave us this #pattern code so I might be totally wrong. I'm no +t very good with pattern yet.. my @nums = split(/:/,$info); #takes out colon where ever it may be in the $info scalar, @nums is a +array with #those values my $word = shift (@nums); # shifts first word my $sum = 0; # $sum is a empty scalar my $var = ($info); # i think $var is supposed to contain the numbers from $info foreach $var (@nums) { $sum = $sum + $var; } #this is to add the numbers together. Sum is empty, so with every new +number #that occurs it goes into sum and adds to whatever number is i +n var. i hope I'm #explaining that right. my %hash = ($word => $sum); #the new sum goes into the hash as a key and the color is in the list, + thats how #they associate with each other.. print @nums;
    Now that I've evaluated the code myself it is definitely missing things.. I just don't know how to build this kind of stuff. I'm such a novice that I had a hard time imagining how to put the pieces together.
        I can't begin to express how much I appreciate your response. Thank you so much, that was SUPER helpful. When you said

        "So do you think the program should stop or keep going if the file can't be opened? Maybe try open second ARGument ?"

        did you mean an "while" loop or a if/or statement? or another ARGV?
      What does this line do  my $info =~ s/(\w+ (-?\d+ )+)//;

        uhm.. its a substitution operator.. for letters and words as well.. so while $info has words and number it substitutes for something.. not sure what though... sorry.