Re: Strange File read problem
by BrowserUk (Patriarch) on Apr 09, 2003 at 15:16 UTC
|
foreach my $str (@lines){print $str, "\n"};
I'm guessing that all the lines are being printed one over the other on the terminal?
Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.
| [reply] [d/l] |
|
|
Possible, but doubtful. He's not using chomp or removing the newline in any other way, so the \n should still be there....
That said, I don't know what the problem is either.
| [reply] [d/l] [select] |
Re: Strange File read problem
by VSarkiss (Monsignor) on Apr 09, 2003 at 15:21 UTC
|
It's a little known fact that the Perl 5 parser will reject any input lines containing the string "Java". That's probably what's getting you.1
Seriously, there's nothing obviously wrong with your program. I would suggest first trying to pin down whether the problem is with reading the file or outputting results. For instance, right after you close the filehandle, add:
print "I got ", scalar(@lines), " from the file\n";
If you get one line, the problem's with the input, and if you get seven lines, it's with the output. At least that will split the program in two, and you can concentrate on half as much code.
Is there anything screwy with the file delimiters? Did you by chance copy it from, say, Unix to Windows in binary mode? Also, if your output is to a shell, try saving it in a file and seeing if there's any funny characters in there.
HTH
1<voice type="cartoon" character="Foghorn Leghorn">That's a joke, son. Yuk it up.</voice>
| [reply] [d/l] |
|
|
OK the problem is with screwy file delimiters. If I open the same file in notepad, then the file appears all in one line with a "rectangle" like character in between my lines.
(funny thing is that if I copy paste that "rectange like" character into this window it results into a new line!!)
I suppose this is a character which textpad and eclipse java editor find OK but notepad and perl do not recognize this as a newline character.
But now how do I solve this problem ? I am kind of stuck...I have two kinds of newlines in my source code and I have to write a utiltity which has to reads all the lines of the source code and process it. Can I find this character and replace it with the one which is universally regarded as a newline?
Please help I am confused!
regards,
Abhishek.
| [reply] |
|
|
Try to find out the hex value of the character first - maybe by using a hex editor on the file(?)
IMO, it'll differ from the newline hexcode
Cheers, MichaelD
| [reply] |
Re: Strange File read problem
by chromatic (Archbishop) on Apr 09, 2003 at 19:08 UTC
|
open FILE, "Sample.java" or die "Cannot open file: $!\n";
while (<FILE>)
{
s/\r/\\r/g;
print "$_\n";
}
close FILE;
| [reply] [d/l] |
|
|
Yes !!! That did the trick!!!
I changed the code to
open FILE, "c:\sample.java";
while(<FILE>)
{
s/\r/\n/g;
print "$_\n";
}
close FILE;
And it printed the file correctly.
thanks for your help. I was so puzzled. How did you figure it out?
regards,
Abhishek.
| [reply] [d/l] |
Re: Strange File read problem
by nothingmuch (Priest) on Apr 09, 2003 at 15:08 UTC
|
here are a few ideas for you to try:
print @lines; - print can take a list. perhaps the problem is in the loop? unlikely. it's probably in the assignment somewere
while (<FILE>){ print } - print the lines as you read them, instead of many at a time. If this works and the prior solution doesn't i suspect something has gone wrong in you assignment to @lines.
This is very odd behavior indeed. The code works perfectly on my machine, and there is nothing in it to go wrong. Perhaps something is wrong with your terminal?
-nuffin zz zZ Z Z #!perl | [reply] [d/l] [select] |
|
|
I created a second file like
package m.y;
import n.a.m.e;
import i.s;
import a.b.h.i.s.h.e.k;
import s.r.i.v.a.s.t.a.v.a;
It worked perfectly as well. so to me it looks that nothing wrong with the terminal or loop.
I am on W2k, active state perl 5.8.0
I print the number of lines at the end of the for loop for Sample.java it says number of lines is 1. for sample2.java it says number of lines is 5.
regards,
Abhishek. | [reply] [d/l] |
|
|
There's obviously somethine wrong with the file. If slurping didn't work, and the same stuff works for other files then the OS is at fault. I think. Perl is not sensitive to the content of the file. The readline library - perhaps you can say that, but generally no. I think the lines are simply not there. Rewrite the file to a new one, and see if the problem returns.
-nuffin zz zZ Z Z #!perl
| [reply] |
Re: Strange File read problem
by derby (Abbot) on Apr 09, 2003 at 15:33 UTC
|
My guess ... the source file you think you're reading is not the source
file you're reading. Try changling the relative Sample.java to the full path
of what you want.
-derby
| [reply] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |