Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Is This Possible?

by jdlev (Scribe)
on Sep 13, 2013 at 12:05 UTC ( #1053904=perlquestion: print w/replies, xml ) Need Help??

jdlev has asked for the wisdom of the Perl Monks concerning the following question:

So I run my pl programs through my command prompt. Well, the amount of data it is processing is just ridiculous. It hasn't ever actually made it to the end of the program. Anyways, is there a way for perl to spit out the max values it finds when you terminate the program by pushing ctrl-c? TIA...
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Is This Possible?
by Perlbotics (Bishop) on Sep 13, 2013 at 12:13 UTC

    Use a signal handler (perlipc).

    use strict; use warnings; my $max; #-- set a signal handler $SIG{INT} = sub { print "Max: $max -- now leaving...\n"; exit 1; }; #-- main while (<>) { # do something that updates $max } print "Max: $max (finally)\n"; #-- normal termination (exit 0)

Re: Is This Possible?
by SuicideJunkie (Vicar) on Sep 13, 2013 at 14:10 UTC

    Why not print "Value so far: $maxVal\r" unless $. % 250; inside your loop?

    With the \r it will overwrite the previous text rather than scrolling madly, and give you a continual update. The $. % 250 keeps it from spending all its time printing. Adjust the variable and value depending on what you are looping over and how fast the loop spins.

      I decided to write to a log interval at interval. Seems to be working splendidly. :)
      I love it when a program comes together - jdhannibal
Re: Is This Possible?
by thezip (Vicar) on Sep 13, 2013 at 16:46 UTC

    You might also consider keeping track of the max value encountered so far, replacing it when a higher max value is found. When this event happens, print a message saying "A new max value of $MAX was found..."

    I would think that this would quickly stop reporting to the screen.

    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011
Re: Is This Possible?
by Marshall (Canon) on Sep 13, 2013 at 13:52 UTC
    Your program is just "not working". You don't have any idea of how the algorithms are functioning.

    Take a small data set and process that and see what that does.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-10-01 13:32 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (13 votes). Check out past polls.