Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Why does this code continue to loop?

by rachard11 (Acolyte)
on May 03, 2017 at 19:35 UTC ( [id://1189436]=perlquestion: print w/replies, xml ) Need Help??

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

Why does the following code process the same file in an infinite loop? I want it to stop processing the file after a single iteration.

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize ; my $mech = WWW::Mechanize->new(); my $txt_url ; my $pdf_url ; my $html_url ; my $output ; my @output ; my $file_content = "D:/Perl/Perl output/garbage_content.csv" ; my $get_file = "http://securities.stanford.edu/filings-case.html?id=10 +1092"; my $filename = "file_output_101092" ; my $response = $mech->get($get_file, ':content_file'=> $file_content,); open my $fh, '<', $file_content ; while (<$fh>) { chomp; my $i =1 ; if(index($_,".txt")>=0) { $txt_url = $_ ; }else { if(index($_,".pdf")>=0) { $pdf_url = $_ ; }else { if(index($_,".html")>=0) { $html_url = $_ ; } } } if (($txt_url)|($pdf_url)|($html_url)) { $output = join ",", $filename, $txt_url, $pdf_url, $html_url ; push @output, ($output) ; foreach (@output) { print "Round $i\n " ; print "$_\n" ; $i++ ; #sleep(1) ; } } } close $fh ;

Replies are listed 'Best First'.
Re: Why does this code continue to loop?
by choroba (Cardinal) on May 03, 2017 at 19:47 UTC
    When I remove the sleep, the program ends in 4 seconds, printing Round 631 as the last line. As the code prints all the output for each round, it prints 1+2+3+4+...+631 "Rounds", i.e. 199396 lines, which means it would take 55 hours with the sleep included.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      OK, I now see it's not an infinite loop, it's just going through the 1+2+3+...+631 Rounds cycle, which wasn't my intent. This code is processing a file with only about a total of 30 instances of html, pdf, or txt references, so I was expecting the @output array to have only about 30 elements. The other 600-ish elements are duplicates. How do I keep each file reference from going into the @output array more than once?

        It only goes into the array once, it gets printed multiple times. Erase this

        foreach (@output) { print "Round $i\n " ; print "$_\n" ; $i++ ; sleep(1) ; }
        and put this at the end of the file.
        foreach (@output) { print "$_\n" ; }

Re: Why does this code continue to loop?
by LanX (Saint) on May 03, 2017 at 19:49 UTC
    Hi

    three questions:

    Why do you sleep 1 ?

    Why do you think it's looping infinitely?

    Why do you expect help without proper indentation of your code?

    update

    See Perltidy or consider a decent editor.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Why does this code continue to loop?
by Anonymous Monk on May 03, 2017 at 19:45 UTC

    Why does the following code process the same file in an infinite loop?

    Hi,

    the loop is not infinite, its just slow (sleep)

Re: Why does this code continue to loop?
by Discipulus (Canon) on May 03, 2017 at 19:53 UTC
    bah, since I arrived 4th I can just add: dormitare de fuga cogitantem vetat..

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Set the dormitory on fire with intelligent vegetables?

        I like this version better than what Google Translate came up with.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

        Not only you don't understand Latin, you also lack the sense of humour and can't google.

        Oh come on, that's a little funny.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-18 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found