Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: A Beginner Needs Homework help

by Swizzlestix617 (Novice)
on Apr 10, 2014 at 21:39 UTC ( [id://1081880]=note: print w/replies, xml ) Need Help??


in reply to Re: A Beginner Needs Homework help
in thread A Beginner Needs Homework help

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.

Replies are listed 'Best First'.
Re^3: A Beginner Needs Homework help
by Anonymous Monk on Apr 10, 2014 at 23:12 UTC
      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?

        did you mean an "while" loop or a if/or statement? or another ARGV?

        I meant in english :)

        See print, see return, see exit, see die

        Main( @ARGV ); exit( 0 ); sub Main { for my $file ( @_ ){ TryAndDoStuffOrDieAndEndProgram( $file ); } } sub TryAndDoStuffOrDieAndEndProgram { open ... or die "Can't open ($file): $! "; ... close ... return; }

        There are other moves than AndEndProgram, like move on to next file, see die/eval

Re^3: A Beginner Needs Homework help
by Anonymous Monk on Apr 10, 2014 at 21:53 UTC
    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.

        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.

        Thank you, thats pretty good ... no need to be sorry :)

        See "Missing explanation" in Re^3: A Beginner Needs Homework help for more about my

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1081880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found