Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Dear monk, I need help with timing out. I have a script that extracts excerpts from financial statements. The basic format is the following.

foreach (@files){ my $excerpt; my $match; my $start1 = time; #measure time for each file my $duration1; my $data = slurp($_); if($data!~m/table\s?of\s?contents?|\sindex\spart\s(?:1|I)/i){ if($data=~m/(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\s?(?:1|I +)\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:Description|DESCRIPTION)?\s?(?:[Oo][Ff +])?\s?(?:[Tt][Hh][Ee])?\s?(?:Busine\s?ss|BUSINE\s?SS|Company|COMPANY) +\s?(?:\.|\-|:|\-\-|\,|\()? (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:\.|\:|\-\-|\-|\,)? +\s?(?:I|1A|1B|2)\s?(?:\.|\:|\-\-|\-|\,)?/x){ $excerpt=$1; $match=3; goto record; } if($data=~m/(?:Business\s?Development|BUSINESS\s?DEVELOPMENT)\ +s? (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\ +s?(?:I|1A|1B|2)\s?(?:\.|\:|\-\-|\-|\,)?/x){ $excerpt=$1; $match=4; goto record; } if($data=~m/(?:PART|Part)\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:I|1)\s? +(?:\.|\-|:|\-\-|\,)?\s?(?:BUSINESS|Business|GENERAL|general) (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:I|1A|1B|2|3)\s?(?: +\:|\-|\,|\-\-|\.|\,)?/x){ $excerpt=$1; $match=5; goto record; } if($data=~m/(?:Item|ITEM)?[Ss]?\s?1\s?(?:\.|\-|\:|\-\-|\,)?(?: +\.|\-|\:|\-\-|\,)?\s?(?:1A)?\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:AND|[Aa]nd| +\&)\s?2\s?(?:\.|\:|\-|\-\-|\,)?\s?(?:\.|\-|\:|\-\-|\,)?\s? (?:[Bb]usiness\s?\,\s?[Rr]isks?\s?[Ff]actors\s?(?:[Aa] +nd|AND|\&)\s?[Pp]roperties|BUSINESS\s?\,\s?RISK\s?FACTORS?\s?AND\s?PR +OPERTIES) (.*?)\s?(?:Item|ITEM)\s?[Ss]?\s?(?:\:|\-|\,|\-\-|\.)?\ +s?(?:1A|1B|2|I|3)\s?(?:\.|\:|\-|\-\-|\,)?/x){ $excerpt=$1; $match=7; goto record; } if($data=~m/(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\s?(?:1|I +)\s?(?:\.|\-|:|\-\-|,)?\s?(?:BUSINESS|[Bb]usiness)\s?(?:\.|\-|\:|\,)? (.*?)\s?(?:Item|ITEM)\s?(?:\.|\:|\-|\-\-|\,)?\s?(? +:I|1A|1B|2)\s?(?:\.|\:|\-|\-\-|\,)?/x){ $match=8; $excerpt=$1; goto record; } } record: if(defined($excerpt)){ $excerpt=~s/\s{2,}|\.\s|\"|\(|\)|\,|\'|\r|\n/ /g; $excerpt=~s/^\s+|\s+$//; #trim $duration1 = ceil((time - $start1)/60); #measure exec +ution time for each file until printing print "$match \n $duration1 \n $excerpt \n"; } }

a lot of times the code gets stuck because it takes time to extract the excerpt as some files are over 10MB. I want to use a timeout function to move to the next element if it takes more than two minutes. I have looked up Alarm but I don't know how to incorporate it into my code. It would be great if you could help me use a timeout in my function in case it gets stuck.


In reply to Help with timeout by eversuhoshin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2014-04-19 08:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (478 votes), past polls