Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

loop by line on a file

by sage.erosenin (Initiate)
on Sep 16, 2013 at 23:10 UTC ( [id://1054348]=perlquestion: print w/replies, xml ) Need Help??

sage.erosenin has asked for the wisdom of the Perl Monks concerning the following question:

Below code loops only once and though i have 5 lines of text in my file it only loops once, how can i get it to loop 5 times, i donot want to store the entire file in memory in an array.
open my $fh,"+<four_comma.txt" or die "Cannot open text $!"; my $i=0; while(<$fh>){ say $_; say ++$i; }

Solution

Got it! Had to change my EOL Conversion on notepad++ to Unix, it was set to DOS / Windows. Thank you.

Replies are listed 'Best First'.
Re: loop by line on a file
by kcott (Archbishop) on Sep 17, 2013 at 00:25 UTC

    G'day sage.erosenin,

    Welcome to the monastery.

    Is "+<four_comma.txt" a valid filename on your OS? It is on mine. Any chance you inadvertently created "+<four_comma.txt" as part of earlier coding, testing, etc.? Check you don't have both "four_comma.txt" and "+<four_comma.txt". Using the three-argument form of open will ensure there's no confusion between mode and filename:

    open my $fh, '+<', 'four_comma.txt' or die ...

    The filename, "four_comma.txt", suggests (at least the possibility) that you've confused array elements with file records. Check your file contents don't look something like this:

    record1,record2,record3,record4,record5

    -- Ken

Re: loop by line on a file
by Old_Gray_Bear (Bishop) on Sep 17, 2013 at 00:06 UTC
    Your code works for me, once I supplied a five line data file. Perhaps your data file is not what you think it is....

    ----
    I Go Back to Sleep, Now.

    OGB

      Well it is called "four_comma.txt"
Re: loop by line on a file
by frozenwithjoy (Priest) on Sep 17, 2013 at 00:09 UTC
    Works fine for me. Can you show your input file? For the following input, I get the following output:

    INPUT:

    a b c d e

    OUTPUT:

    a 1 b 2 c 3 d 4 e 5
Re: loop by line on a file
by nevdka (Pilgrim) on Sep 17, 2013 at 01:15 UTC

    If you want to loop through your entire file 5 times, then you can use seek to get back to the start of the file, then put everything inside another loop:

    open my $fh,"+<four_comma.txt" or die "Cannot open text $!"; my $i=0; for (1..5) { while(<$fh>){ say $_; say ++$i; } seek $fh, 0, 0; }
Re: loop by line on a file
by ww (Archbishop) on Sep 17, 2013 at 11:25 UTC
    doctext of OP (thanks to Corion's way-back machine) reformatted for easier reading:

    Below code loops only once and though i have 5 lines of text in my file it only loops once,
    how can i get it to loop 5 times,
    i donot want to store the entire file in memory in an array.

    open my $fh,"+<four_comma.txt" or die "Cannot open text $!"; my $i=0; while(<$fh>){ say $_; say ++$i; }
Re: loop by line on a file
by smls (Friar) on Sep 16, 2013 at 23:48 UTC
    What do you mean "it only loops once"? The code does loop through the entire input file, line by line.
Re: loop by line on a file
by sage.erosenin (Initiate) on Sep 17, 2013 at 05:23 UTC
    My input file has the following contents, however i think i know wha the problem is, although i have the content in four different lines in notepad++ it looks like there is not /r/n character at the end of every line. How do i format my data ? Even when i try and paste data here it comes up all in one line

      got it, i had to change the EOL conversion on notepad++ to UNIX from DOS/Windows, Thank you.

        Thanks for letting us know your solution. Could you also please add back the original question (w/ your solution as an update) so others can learn from your work in the future? Thanks!
Re: loop by line on a file
by sundialsvc4 (Abbot) on Sep 17, 2013 at 12:52 UTC

    Please don’t alter the original-post after a problem is solved.   Now, no one can look back at this thread and have any idea whatsoever what the problem was, or how it was fixed.

      Apologies, still finding my way through working with perlmonks, Its a little different than other forum templates.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found