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:
using text file lines to create a list

 



tazengr
New User

Jul 6, 2010, 10:34 PM

Post #1 of 2 (672 views)
using text file lines to create a list Can't Post

Hello,
I am trying to take the lines of a text file and turn them into an array variable.
My text file is called log.txt and is a simple text file with the following lines:
line_1
line_2
line_3
I would like to take each of these lines and place them into an array variable called @array_variable, so that each element corresponds to a line of the text file. As in $array_variable[0]="line_1" and so on.

So I tried to the following code:

#!/usr/bin/perl
use warnings;
use strict;
open (MYINPUTFILE, "<log.txt");
while(<MYINPUTFILE>){
my $line = $_;
}

However I can only get the variable $line to equal the corresponding line of the text file within the while loop. However when I call it from outside the while loop, Perl complains that $line requires an explicit package name.

Does anyone know how I can take the contents of a text file, line-by-line, and convert them into an array variable for Perl.

Thanks,
TazEngr


tazengr
New User

Jul 7, 2010, 6:16 AM

Post #2 of 2 (662 views)
Re: [tazengr] using text file lines to create a list [In reply to] Can't Post

ok actually I got the answer.
I was going about it the wrong way.

#!/usr/bin/perl
use warnings;
use strict;
open (MYINPUTFILE, "<log.txt");
my @raw_data=<MYINPUTFILE>;

The code above makes the array @raw_data equal to an array list whose elements are the lines of the text file, log.txt.

Thanks,
TazEngr

 
 


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

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