Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Subtraction code needed

by PilotinControl (Pilgrim)
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); } }

Replies are listed 'Best First'.
Re: Subtraction code needed
by NetWallah (Canon) 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 (Archbishop) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028785]
Approved by NetWallah
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-05-28 03:06 GMT
Find Nodes?
    Voting Booth?