Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Subtraction code needed

by PilotinControl (Scribe)
on Apr 16, 2013 at 00:22 UTC ( #1028785=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good evening monks! How do I go about subtracting the total count list once the total is met for the following:
Total Road Crews Needed: 3
and each time a crew is added the total number decreases by 1
Which should read: Total Road Crews Needed: 2
The code I use to add a crew is below:

sub addcrewmember { print colored ("| PLEASE ENTER YOUR NAME: |\n"); my $entyourname = <STDIN>; $entyourname = <STDIN> until defined $entyourname; chomp $entyourname; cls(); print ("| PLEASE ENTER CREW: |\n"); my $entyourpos = <STDIN>; $entyourpos = <STDIN> until defined $entyourpos; chomp $entyourpos; cls(); open (DAT,'>>crewmemberdata.txt') or die "$!\n"; print DAT ($entyourname); print DAT (":"); print DAT ($entyourpos); print DAT ("\n"); close (DAT); }

And the code I use to view all the crew members is below:

sub viewallcrew { Title "View All Crew Members"; no warnings 'redefine'; no warnings 'uninitialized'; open(MYINPUTFILE, "crewmemberdata.txt"); # open for input $| = 1; my @lines = <MYINPUTFILE>; # read file into list close (MYINPUTFILE); foreach (@lines) { chomp; my ($VcrewName, $VposDesired) = split(":"); $VcrewName = "" if !defined($VcrewName); $VposDesired = "" if !defined($VposDesired); my $format = " %-14s %-14s\n"; printf ($format, $VcrewName, $VposDesired); } }

Comment on Subtraction code needed
Select or Download Code
Re: Subtraction code needed
by NetWallah (Abbot) on Apr 16, 2013 at 03:20 UTC
    Your problem statement is somewhat fuzzy.

    I'm guessing you are just looking for a way to decrement, and show remaining count.

    To do this, re-write your "for" loop like this:

    my $crew_count = 3; # Hard-code, or read from another file. foreach (@lines) { chomp; my ($VcrewName, $VposDesired) = split(":"); $VcrewName = "" if !defined($VcrewName); $VposDesired = "" if !defined($VposDesired); my $format = " %-14s %-14s\n"; printf ($format, $VcrewName, $VposDesired); $crew_count --; # Decrement the count } print "Total Road Crews Needed: $crew_count\n"

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Solved Thanks!

Re: Subtraction code needed
by ww (Bishop) on Apr 16, 2013 at 05:34 UTC
    Your narrative is hard to interpret (my WAG is that you want to keep a count of the nmumber of $entyourname in the various <c>$entyourposv and close the program when some one of those reaches a predetermined count). Think hash.

    Just as bad is the fact that your code won't compile: what makes you think "colored" or "cls()" are recognized/used by Perl?. Please do not post uncompilable code unless your question is specifically about why it won't compile and/or the associated error message.

    And... I've lost count of the times where you've posted what appear to be primer level questions without any visible attempt to solve your own problems. It's really time to try to learn something instead of cadging solutions for your school, hobby or (Heaven Forefend) $job.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Actually Colored and Cls (clear screen) is Win::32 perl modules using ActiveState Perl and yes all my questions posted have been answered...some from kind folks here and the rest on my own.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028785]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2014-07-11 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (232 votes), past polls