LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read a char from a file (PERL) (https://www.linuxquestions.org/questions/programming-9/read-a-char-from-a-file-perl-275353/)

linuxlover1 01-08-2005 09:11 AM

Read a char from a file (PERL)
 
Hello everyone !

I am trying to read from a textfile a charachter one bye one and then write it to another file.
I am trying to figure it out using perl.
I did it in pascal , which looks like

Var
ch:char;
.
.
.
BEGIN
.
.
.
assign(input1,input2);
reset(input1);
assign(output1,output2);
rewrite(output1);

While not eof( INPUT1 ) do
begin
while not eoln( INPUT1 ) do
begin
read(input1,ch);
write(output1,ch);
end
end
close(input1);close(output1);
.
.
.
END

In perl how do i work with only one charachter each time in the loop ??
How do i work with files in perl ?? in perl there arent the: "EOF" and "EOLN" commands to indicate the end of the file AND the end of the line. Is there sth simmilar ??
How do i built the above nested loop in perl ?

Thx for your help !

domquem 01-08-2005 10:57 AM

try this :
http://www.htmlite.com/perl024a.php
http://www.elated.com/tutorials/prog...ng_with_files/
:)

linuxlover1 01-09-2005 03:16 AM

thx for the help dude !
But is there any way , that i can read only one char each time ?

Cedrik 01-09-2005 04:57 AM

Here is a way to access a file char by char :
Code:

# open the file
open MYFILE, "/path/to/file" or die "could not open /path/to/file";

# loop line by line until EOF
while(<MYFILE>) {
    #split line in a char array
    @chars = split //;
   
    #loop char by char
    for $char(@chars) { print "$char\n"; }
}

# close the file
close MYFILE;


linuxlover1 01-09-2005 09:10 AM

Thx a lot man !


All times are GMT -5. The time now is 11:17 PM.