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

Remove a Line

by PilotinControl (Scribe)
on Mar 31, 2013 at 20:30 UTC ( #1026383=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon oh wise Monks I do have a question and the code is posted below

sub railcarcount { my %count; Title "Total Railcar Count"; open(INPUT, "<railcardata.txt"); open(OUTPUT, ">railcarcountdata.txt"); my $railcarcnt='Boxcar|Reefer|Flatcar|Tankcar|Gondola|Hopper'; while(<INPUT>){ my @words = split(/\:/); foreach my $word (@words){ if($word=~/($railcarcnt)/io){ $count{$1}++; } } } print colored ("------------------------------------------------------ +--------------------------",'green bold'); print colored (" TOTAL RAILCARS LISTED BELOW + ",'green bold'); print colored ("------------------------------------------------------ +--------------------------\n\n\n\n",'green bold'); foreach my $word (sort keys %count) { printf("%42s %-16s %-23s %6s", colored("There are",'cyan bold'), colored("$count{$word}",'yellow bold'), colored("$word",'cyan bold'), colored("Car(s)\n",'cyan bold') ); print OUTPUT "There are $count{$word} $word Car(s)\n"; } close INPUT; close OUTPUT; print colored ("\n\n\n\n\n WHEN YOU ARE DONE VIEWING + HIT RETURN TWICE:\n\n\n", 'green bold'); my $input = <STDIN>; $input = <STDIN> until defined $input; chomp($input=<STDIN>); cls(); structure::railcar(); }

__DAT__

B&LE:65065:Hopper:Home
B&LE:65700:Hopper:Home
VM:51142:Hopper:Home
OS:1400:Hopper:Home

__OUTPUT__

There are 16 Car(s)
There are 4 Hopper Car(s)

What I "think" it is doing is counting all the words and giving a total up top....that output is not relevant...only the car count. How do I remove that part? Thanks!

Comment on Remove a Line
Download Code
Re: Remove a Line
by CountZero (Bishop) on Mar 31, 2013 at 21:00 UTC
    Simply delete lines 25 to 29 and the message with the total number of cars will no longer be printed.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      No that just keeps the output from going to the terminal screen...the file still reads that line: There are Car(s)

        What file?
        Now you have me totally confused.

        What do you mean by "the file still reads that line". Which file reads which line?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Remove a Line
by poj (Priest) on Apr 01, 2013 at 08:44 UTC
    I suggest you add a print line to understand what is happening. It might be a data issue.
    if ($word=~/($railcarcnt)/io){ $count{$1}++; print "Line $. [$word] matches [$1]\n"; # add }
    poj
Re: Remove a Line
by CountZero (Bishop) on Apr 01, 2013 at 09:24 UTC
    OK. I had another look at your program.

    I rewrote it and left out all the fancy formatting (which really put me on the wrong track) and brought it down to the bare minimum.

    As you will see when you run the program, it works perfectly and the output is as expected:

    use Modern::Perl; my %count; my $railcarcnt = qr(Boxcar|Reefer|Flatcar|Tankcar|Gondola|Hopper); while (<DATA>) { $count{$1}++ if /($railcarcnt)/i; } foreach my $word ( sort keys %count ) { say "There are $count{$word} $word Car(s)"; } __DATA__ B&LE:65065:Hopper:Home B&LE:65700:Hopper:Home VM:51142:Hopper:Home OS:1400:Hopper:Home HT&SF:1234:Flatcar:Away NMBS:999:Reefer:Gone
    Output:
    There are 1 Flatcar Car(s) There are 4 Hopper Car(s) There are 1 Reefer Car(s)
    I can only guess that there must be a data issue or that the code you posted is not the code that actually ran to get you that output.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      I fixed the issue...it was a typo in the code...in the $railcarcnt section I had an extra " | " in the line which threw everything off as the code was looking for additional data which it would of never found or have been inputted..after awhile it all seems to run together....in the middle of the night. Thanks for the input.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-18 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (50 votes), past polls