?
Solved

Multiple line input into an array variable

Posted on 1998-12-03
4
Medium Priority
?
198 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 400 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

Industry Leaders: 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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

741 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