CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Reading a list from a text file

 



SteveT
Deleted

Jun 28, 2000, 8:39 AM

Post #1 of 6 (641 views)
Reading a list from a text file Can't Post

I want to be able to read data from a text file. For example I have a file input.txt which contains:

Bob
Joe
Sue

What is the simplest way to open this file and read each name into a variable.

Thanks


monocle
User

Jun 28, 2000, 9:25 AM

Post #2 of 6 (641 views)
Re: Reading a list from a text file [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(LIST, "input.txt");
@LINES = <LIST>;
close(LIST);
</pre><HR></BLOCKQUOTE>
Now each line of the file will be in the array @LINES

you can access them as so:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $line (@LINES){
#do something with each $line like split, print, evaluate, etc.
}
</pre><HR></BLOCKQUOTE>
or
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$line_1 = @LINES[0];
$line_2 = @LINES[1];
$line_3 = @LINES[2];
#Keep in mind that array counts begin with element 0
</pre><HR></BLOCKQUOTE>

you can a whole lot more but you asked for simple

Hope this helps


------------------
Monocle
Hear great techno music by Monocle at http://www.mp3.com/monocle. CD now on sale!



SteveT
Deleted

Jun 28, 2000, 9:49 AM

Post #3 of 6 (641 views)
Re: Reading a list from a text file [In reply to] Can't Post

This is perfect - Thanks!

Steve T.


simon
Deleted

Jun 30, 2000, 11:10 AM

Post #4 of 6 (641 views)
Re: Reading a list from a text file [In reply to] Can't Post

Sorry about this, I am very a newbie.
In the input.txt he mentioned would he not have to put the information in this format:
bob joe sue
instead of
bob
joe
sue
The reason why i ask this is because so far every database (*.txt) file i have seen, the information is listed in rows one after each other. Please explain this to me.
Regards:
Simon -


monocle
User

Jun 30, 2000, 12:50 PM

Post #5 of 6 (641 views)
Re: Reading a list from a text file [In reply to] Can't Post

The first piece of code opens the file and reads it into the array. By default, it will place each line of the file into a seperate element of the array. Therefore by steping through the array, element by element, you are basicly steping through the file, line by line.

If each line were to conatin mulitiple pieces of info like so bob:dole:64:male you would then perform some sort of split operation on each line as you step through the array. This would be done in one of the other pieces of code mentioned.

hope that made things clearer. Smile


------------------
Monocle
Hear great techno music by Monocle at http://www.mp3.com/monocle. CD now on sale!



kade
Deleted

Jun 30, 2000, 8:13 PM

Post #6 of 6 (641 views)
Re: Reading a list from a text file [In reply to] Can't Post

I faced this problem 4 weeks ago.
If You want input.txt to be like this;
Bob
Joe
Sue
#instead of#
Bob Joe Sue
The script for writing input.txt should be;
open(FILE,">>input.txt");
print FILE "$input{name}\n" #\n make newline
close (FILE);
#that's it,I hope this help!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives