Solved

Multiple line input into an array variable

Posted on 1998-12-03
4
185 Views
Last Modified: 2010-03-04
I'm trying to read in, from a file, multiple lines into an array variable... Lets call it @temp.  If I do @temp = <FILE>;  Than it seperates the array by newlines.  Is there a way to prevent this?  Or another way to read in the data that prevents this?  

Thanks...
0
Comment
Question by:willpd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1206715
The basic answer is to:

undef $/;

Then @temp = <FILE> will put all of <FILE> into $temp[0].  However, I wonder if you might want each the first line into $temp[0], the next into $temp[1], and so on.  If so, you could do:

undef $/;
open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp = split("\n", <FILE>);
close(FILE);

On the other hand, you could also do:
open(FILE, "<$filename") || die "Unable to open $filename: $!";
push(@temp, <FILE>);
close(FILE);

And the most basic of all is
open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp= <FILE>;
close(FILE);

On the other hand, maybe you really do want the entire file into zero'th element of the array....  (But, really, for maintainability's sake, you really ought say
$/=0;
open(FILE, "<$filename") || die "Unable to open $filename: $!";
$temp[0] = <FILE>;
close(FILE);



0
 
LVL 84

Expert Comment

by:ozo
ID: 1206716
The simplest way to prevent it would be not to do it.
But that begs the question of what you would prefer to be doing instead
( $/=0; seems rather less likely to be what was wanted than undef $/;)
0
 

Author Comment

by:willpd
ID: 1206717
Thanks, I think the undef $/; and then splitting will work for me!

Thanks again!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206718
No, if you're going to use that approach, my point was that leaving $/ alone, and using

open(FILE, "<$filename") || die "Unable to open $filename: $!";
@temp= <FILE>;
close(FILE);


is more efficient (as well as clearest).

And yes, it was a brain cramp that made me type $/=0 (Anyone got an ace bandage?)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question