Solved

Multiple line input into an array variable

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now