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
| [reply] [d/l] [select] |
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
| [reply] |
|
Well it is called "four_comma.txt"
| [reply] |
|
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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;
}
| [reply] [d/l] |
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;
}
| [reply] [d/l] |
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. | [reply] |
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
| [reply] |
|
| [reply] |
|
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!
| [reply] |
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.
| [reply] |