Solved

Multiple line input into an array variable

Posted on 1998-12-03
4
180 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

829 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