LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2005, 09:11 AM   #1
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Rep: Reputation: 15
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 !
 
Old 01-08-2005, 10:57 AM   #2
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Rep: Reputation: 0
try this :
http://www.htmlite.com/perl024a.php
http://www.elated.com/tutorials/prog...ng_with_files/
 
Old 01-09-2005, 03:16 AM   #3
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Original Poster
Rep: Reputation: 15
thx for the help dude !
But is there any way , that i can read only one char each time ?
 
Old 01-09-2005, 04:57 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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;
 
Old 01-09-2005, 09:10 AM   #5
linuxlover1
Member
 
Registered: Jun 2003
Location: UK
Posts: 54

Original Poster
Rep: Reputation: 15
Thx a lot man !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: Best way to read an entire text file and putting it in a *char. Claus Programming 8 03-15-2004 01:22 PM
Perl Program That Read From .conf File Crashed_Again Programming 2 12-07-2003 06:49 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Perl : Changing a single char in a string richhill Programming 2 09-17-2003 04:31 PM
perl: substring extraction after specific char markus1982 Programming 2 10-06-2002 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration